Java - 将 int 更改为 ascii
java 有没有办法将 int 转换为 ascii 符号?
Is there a way for java to convert int's to ascii symbols?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
java 有没有办法将 int 转换为 ascii 符号?
Is there a way for java to convert int's to ascii symbols?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
是否要将
int
转换为char
?:或者是否要将
int
转换为String
是?或者你说的是什么意思?
Do you want to convert
int
s tochar
s?:Or do you want to convert
int
s toString
s?Or what is it that you mean?
如果您首先将 int 转换为 char,您将获得 ascii 代码。
例如:
If you first convert the int to a char, you will have your ascii code.
For example:
将 int 转换为 ASCII 的方法有很多种(取决于您的需要),但这里有一种将每个整数字节转换为 ASCII 字符的方法:
,“TEST”的 ASCII 文本可以表示为字节数组:
例如 你可以执行以下操作:
...所以这实际上将 32 位整数中的 4 个字节转换为 4 个单独的 ASCII 字符(每个字节一个字符)。
There are many ways to convert an int to ASCII (depending on your needs) but here is a way to convert each integer byte to an ASCII character:
For example, the ASCII text for "TEST" can be represented as the byte array:
Then you could do the following:
...so this essentially converts the 4 bytes in a 32-bit integer to 4 separate ASCII characters (one character per byte).
您可以在 java 中将数字转换为 ASCII。将数字 1(基数为 10)转换为 ASCII 的示例。
输出:
You can convert a number to ASCII in java. example converting a number 1 (base is 10) to ASCII.
Output:
tl;dr
使用
Character#toString
,而不是char
。像这样:示例:
tl;dr
Use
Character#toString
, notchar
. Like this:Example:
char
is legacyThe
char
type in Java is legacy, and is essentially broken. As a 16-bit value,char
is incapable of representing most characters defined by Unicode.This succeeds:
This fails:
See code run live at IdeOne.com.
Code point
Use code point integer numbers to represent individual letters.
US-ASCII is a subset of Unicode. So, any US-ASCII number (0-127) is also a Unicode code point (0-1,114,111).
To change a code point number to a
String
object containing a single character, callCharacter#toString
.See this code run live at IdeOne.com.
事实上在最后一个答案中
String strAsciiTab = Character.toString((char) iAsciiValue);
重要部分是 (char)iAsciiValue 正在完成这项工作(Character.toString 无用)
意味着第一个答案实际上是正确的
char ch = (char) yourInt;
如果 yourint=49(或 0x31),则 ch 将为“1”
In fact in the last answer
String strAsciiTab = Character.toString((char) iAsciiValue);
the essential part is (char)iAsciiValue which is doing the job (Character.toString useless)
Meaning the first answer was correct actually
char ch = (char) yourInt;
if in yourint=49 (or 0x31), ch will be '1'
在 Java 中,您确实想使用 Integer.toString 将整数转换为其对应的字符串值。如果您只处理数字 0-9,那么您可以使用如下所示的内容:
或者,等效地:
In Java, you really want to use Integer.toString to convert an integer to its corresponding String value. If you are dealing with just the digits 0-9, then you could use something like this:
Or, equivalently:
最简单的方法是使用类型转换:
The most simple way is using type casting:
最简单的方法是获取整数并使用强制转换运算符
前任
The most simple way is to get integer and just use the casting operator
Ex