【发布时间】:2024-01-14 14:00:01
【问题描述】:
java有没有办法将int转换为ascii符号?
【问题讨论】:
java有没有办法将int转换为ascii符号?
【问题讨论】:
有很多方法可以将 int 转换为 ASCII(取决于您的需要),但这里有一种将每个整数字节转换为 ASCII 字符的方法:
private static String toASCII(int value) {
int length = 4;
StringBuilder builder = new StringBuilder(length);
for (int i = length - 1; i >= 0; i--) {
builder.append((char) ((value >> (8 * i)) & 0xFF));
}
return builder.toString();
}
例如,“TEST”的 ASCII 文本可以表示为字节数组:
byte[] test = new byte[] { (byte) 0x54, (byte) 0x45, (byte) 0x53, (byte) 0x54 };
然后您可以执行以下操作:
int value = ByteBuffer.wrap(test).getInt(); // 1413829460
System.out.println(toASCII(value)); // outputs "TEST"
...所以这实际上将 32 位整数中的 4 个字节转换为 4 个单独的 ASCII 字符(每个字节一个字符)。
【讨论】:
要将ints 转换为chars 吗?:
int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !
还是要将ints 转换为Strings?
int yourInt = 33;
String str = String.valueOf(yourInt);
或者你的意思是什么?
【讨论】:
您可以在 java 中将数字转换为 ASCII。将数字 1(基数为 10)转换为 ASCII 的示例。
char k = Character.forDigit(1, 10);
System.out.println("Character: " + k);
System.out.println("Character: " + ((int) k));
输出:
Character: 1
Character: 49
【讨论】:
如果您首先将 int 转换为 char,您将获得您的 ascii 代码。
例如:
int iAsciiValue = 9; // Currently just the number 9, but we want Tab character
// Put the tab character into a string
String strAsciiTab = Character.toString((char) iAsciiValue);
【讨论】:
其实在最后一个答案 String strAsciiTab = Character.toString((char) iAsciiValue); 最重要的部分是 (char)iAsciiValue,它正在完成这项工作(Character.toString 无用)
意思是第一个答案实际上是正确的 char ch = (char) yourInt;
如果在 yourint=49(或 0x31)中,ch 将为 '1'
【讨论】:
在Java 中,您确实想使用Integer.toString 将整数转换为其对应的字符串值。如果您只处理数字 0-9,那么您可以使用以下内容:
private static final char[] DIGITS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
private static char getDigit(int digitValue) {
assertInRange(digitValue, 0, 9);
return DIGITS[digitValue];
}
或者,等效地:
private static int ASCII_ZERO = 0x30;
private static char getDigit(int digitValue) {
assertInRange(digitValue, 0, 9);
return ((char) (digitValue + ASCII_ZERO));
}
【讨论】:
最简单的方法是使用类型转换:
public char toChar(int c) {
return (char)c;
}
【讨论】:
使用Character#toString,而不是char。
String result = Character.toString( yourAsciiNumber ) ;
例如:
Character.toString( 97 ) // LATIN SMALL LETTER A
一个
Character.toString( 128_567 ) // FACE WITH MEDICAL MASK
?
char 是旧版Java 中的char 类型是遗留的,本质上已被破坏。作为16-bit 值,char 无法代表Unicode 定义的大多数字符。
这成功了:
System.out.println( Character.toString( 128_567 )); // Unicode code points handle full-range of Unicode characters.
?
这失败了:
System.out.println( ( char ) 128_567 ); // `char` fails with most Unicode characters.
使用code point整数表示单个字母。
US-ASCII 是 Unicode 的一个子集。因此,任何 US-ASCII 数字 (0-127) 也是一个 Unicode 代码点 (0-1,114,111)。
要将代码点编号更改为包含单个字符的 String 对象,请调用 Character#toString。
String x = Character.toString( 97 ) ;
一个
【讨论】:
最简单的方法是获取整数并使用强制转换运算符 例
int num = 33;
System.out.println((char) num); //Outputs 33
//if you want to find the integer value of character instead.
//Just do the reverse
char ch = '%';
System.out.println((int) ch);
【讨论】: