Java 我们可以将整数转换为字符吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can we convert integer into character
提问by higherDefender
we can convert character to an integer equivalent to the ASCII value of the same but can we do the reverse thing ie convert a given ASCII value to its character equivalent?
我们可以将字符转换为与相同的 ASCII 值等效的整数,但我们可以做相反的事情,即将给定的 ASCII 值转换为其等效的字符吗?
public String alphabets(int[] num)
{
char[] s = new char[num.length];
String str = new String();
for(int i=0; i< num.length; i++)
{
s[i] = 'A' + (char)(num[i]- 1);
str += Character.toString(s[i]);
}
return str;
}
shows possible lost of precision error ...
显示可能丢失精度误差...
采纳答案by Justin Ethier
To convert to/from:
转换为/从:
int i = 65;
char c = (char)i;
char c = 'A';
int i = (int)c;
回答by Donal Fellows
Doesn't casting to char
work?
不投射char
工作吗?
回答by GorillaApe
There are several ways.
Look at the Character wrapper class.
Character.digit()
may do the trick.
Actually this does the trick!!
有几种方法。查看 Character 包装类。
Character.digit()
可能会起作用。其实这个就行了!!
Integer.valueOf('a')
回答by dtb
public static char[] toChars(int codePoint)
Converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array.
public static char[] toChars(int codePoint)
将指定的字符(Unicode 代码点)转换为其存储在 char 数组中的 UTF-16 表示。
回答by Bozho
You actually don't even need a cast:
你实际上甚至不需要演员:
char c = 126;
And this actually appears to work for unicode characters as well. For example try:
这实际上似乎也适用于 unicode 字符。例如尝试:
System.out.println((int) '?'); // outputs 3650, a thai symbol
char p = 3650;
System.out.println(p); // outputs the above symbol
回答by matsev
The error is more complex than you would initially think, because it is actually the '+' operator that causes the "possible loss of precision error". The error can be resolved if the cast is moved:
该错误比您最初想象的要复杂,因为实际上是“+”运算符导致了“可能的精度误差损失”。如果演员被移动,错误可以解决:
s[i] = (char)('A' + (num[i]- 1));
Explanation
In the first bullet list of §5.6.2 Binary Numeric Promotionin the Java Language Specificationit is stated that:
说明
在第一个项目符号列表§5.6.2二元数值提升在Java语言规范中指出,:
When an operator applies binary numeric promotion to a pair of operands [...] the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:
- If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
- If either operand is of type double, the other is converted to double.
- Otherwise, if either operand is of type float, the other is converted to float.
- Otherwise, if either operand is of type long, the other is converted to long.
- Otherwise, both operands are converted to type int.
当运算符将二进制数字提升应用于一对操作数 [...] 时,以下规则按顺序应用,根据需要使用扩展转换(第 5.1.2 节)来转换操作数:
- 如果任何操作数是引用类型,则执行拆箱转换(第 5.1.8 节)。然后:
- 如果任一操作数的类型为 double,则另一个将转换为 double。
- 否则,如果任一操作数的类型为 float,则另一个将转换为 float。
- 否则,如果任一操作数的类型为 long,则另一个将转换为 long。
- 否则,两个操作数都被转换为 int 类型。
In the next bullet list it is stated that:
在下一个项目符号列表中指出:
Binary numeric promotion is performed on the operands of certain operators:
- The multiplicative operators *, / and % (§15.17)
- The addition and subtraction operators for numeric types + and - (§15.18.2)
- The numerical comparison operators , and >= (§15.20.1)
- The numerical equality operators == and != (§15.21.1)
- The integer bitwise operators &, ^, and | (§15.22.1)
- In certain cases, the conditional operator ? : (§15.25)
对某些运算符的操作数执行二进制数字提升:
- 乘法运算符 *、/ 和 %(第 15.17 节)
- 数字类型 + 和 - 的加法和减法运算符(第 15.18.2 节)
- 数值比较运算符 , 和 >= (§15.20.1)
- 数值相等运算符 == 和 !=(第 15.21.1 节)
- 整数按位运算符 &、^ 和 | (第 15.22.1 节)
- 在某些情况下,条件运算符 ? :(第 15.25 节)
In your case, that translates to:
在你的情况下,这转化为:
s[i] = (int)'A' + (int)((char)(num[i] - (int)1));
因此错误。