java的int转为char_在java中将int转换为char (Convert int to char in java)

最佳答案

英文原文

int a = 1;

char b = (char) a;

System.out.println(b);

hola, well i went through the same problem but what i did was the following code.

int a = 1

char b = Integer.toString(a).charAt(0);

System.out.println(b);

With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.

Sorry if my english isn't well explained, hope it helps you.

中文翻译

int a = 1;

char b =(char)a;

的System.out.println(b)中;

hola,我遇到了同样的问题,但我做的是以下代码。

int a = 1

char b = Integer.toString(a).charAt(0);

的System.out.println(b)中;

使用此方法,您将获得十进制值作为char类型。我使用索引为0的charAt(),因为该字符串中唯一的值是'a',如您所知,该字符串中'a'的位置从0开始。

很抱歉,如果我的英语没有得到很好的解释,希望对你有所帮助。

int a = 1;

char b = (char) a;

System.out.println(b);

hola, well i went through the same problem but what i did was the following code.

int a = 1

char b = Integer.toString(a).charAt(0);

System.out.println(b);

With this you get the decimal value as a char type. I used charAt() with index 0 because the only value into that String is 'a' and as you know, the position of 'a' into that String start at 0.

Sorry if my english isn't well explained, hope it helps you.

int a = 1;

char b =(char)a;

的System.out.println(b)中;

hola,我遇到了同样的问题,但我做的是以下代码。

int a = 1

char b = Integer.toString(a).charAt(0);

的System.out.println(b)中;

使用此方法,您将获得十进制值作为char类型。我使用索引为0的charAt(),因为该字符串中唯一的值是'a',如您所知,该字符串中'a'的位置从0开始。

很抱歉,如果我的英语没有得到很好的解释,希望对你有所帮助。

参考答案2

int a = 1;

char b = (char) a;

System.out.println(b);

will print out the char with ascii value 1 (start-of-heading char, which isn't printable).

int a = '1';

char b = (char) a;

System.out.println(b);

will print out the char with ascii value 49 (one corresponding to '1')

If you want to convert a digit (0-9), you can add 48 to it and cast, or something like Character.forDigit(a, 10);.

If you want to convert an int as in ascii value, you can use Character.toChars(48) for example.

参考答案3

My answer is similar to jh314's answer but I'll explain a little deeper.

What you should do in this case is:

int a = 1;

char b = (char)(a + '0');

System.out.println(b);

Here, we used '0' because chars are actually represented by ASCII values. '0' is a char and represented by value of 48.

We typed (a + '0') and in order to add these up, Java converted '0' to its ASCII value which is 48 and a is 1 so the sum is 49. Then what we did is:

(char)(49)

We casted int to char. ASCII equivalent of 49 is '1'. You can convert any digit to char this way and is smarter and better way than using .toString() method and then substracting the digit by .charAt() method.

参考答案4

It seems like you are looking for the Character.forDigit method:

final int RADIX = 10;

int i = 4;

char ch = Character.forDigit(i, RADIX);

System.out.println(ch); // Prints '4'

There is also a method that can convert from a char back to an int:

int i2 = Character.digit(ch, RADIX);

System.out.println(i2); // Prints '4'

Note that by changing the RADIX you can also support hexadecimal (radix 16) and any radix up to 36 (or Character.MAX_RADIX as it is also known as).

参考答案5

Nobody has answered the real "question" here: you ARE converting int to char correctly; in the ASCII table a decimal value of 01 is "start of heading", a non-printing character. Try looking up an ASCII table and converting an int value between 33 and 7E; that will give you characters to look at.

参考答案6

In java a char is an int. Your first snippet prints out the character corresponding to the value of 1 in the default character encoding scheme (which is probably Unicode). The Unicode character U+0001 is a non-printing character, which is why you don't see any output.

If you want to print out the character '1', you can look up the value of '1' in the encoding scheme you are using. In Unicode this is 49 (the same as ASCII). But this will only work for digits 0-9.

You might be better off using a String rather than a char, and using Java's built-in toString() method:

int a = 1;

String b = toString(a);

System.out.println(b);

This will work whatever your system encoding is, and will work for multi-digit numbers.

参考答案7

Whenever you type cast integer to char it will return the ascii value of that int (once go through the ascii table for better understanding)

int a=68;

char b=(char)a;

System.out.println(b);//it will return ascii value of 68

//output- D

参考答案8

if you want to print ascii characters based on their ascii code and do not want to go beyond that (like unicode characters), you can define your variable as a byte, and then use the (char) convert. i.e.:

public static void main(String[] args) {

byte b = 65;

for (byte i=b; i<=b+25; i++) {

System.out.print((char)i + ", ");

}

BTW, the ascii code for the letter 'A' is 65

参考答案9

If you want to convert a character to its corresponding integer, you can do something like this:

int a = (int) 'a';

char b = (char) a;

System.out.println(b);

This happens because in ASCII there are some items that can not be printed normally.

For example, numbers 97 to 122 are integers corresponding to the lowercase letters a to z.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值