Java - 将 int 更改为 ascii

发布于 2024-10-22 18:20:50 字数 36 浏览 3 评论 0原文

java 有没有办法将 int 转换为 ascii 符号?

Is there a way for java to convert int's to ascii symbols?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

黑凤梨 2024-10-29 18:20:50

是否要将 int 转换为 char?:

int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !

或者是否要将 int 转换为 String是?

int yourInt = 33;
String str = String.valueOf(yourInt);

或者你说的是什么意思?

Do you want to convert ints to chars?:

int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !

Or do you want to convert ints to Strings?

int yourInt = 33;
String str = String.valueOf(yourInt);

Or what is it that you mean?

dawn曙光 2024-10-29 18:20:50

如果您首先将 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);

If you first convert the int to a char, you will have your ascii code.

For example:

    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);
ま昔日黯然 2024-10-29 18:20:50

将 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 字符(每个字节一个字符)。

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:

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();
}

For example, the ASCII text for "TEST" can be represented as the byte array:

byte[] test = new byte[] { (byte) 0x54, (byte) 0x45, (byte) 0x53, (byte) 0x54 };

Then you could do the following:

int value = ByteBuffer.wrap(test).getInt(); // 1413829460
System.out.println(toASCII(value)); // outputs "TEST"

...so this essentially converts the 4 bytes in a 32-bit integer to 4 separate ASCII characters (one character per byte).

羁拥 2024-10-29 18:20:50

您可以在 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

You can convert a number to ASCII in java. example converting a number 1 (base is 10) to ASCII.

char k = Character.forDigit(1, 10);
System.out.println("Character: " + k);
System.out.println("Character: " + ((int) k));

Output:

Character: 1
Character: 49
捎一片雪花 2024-10-29 18:20:50

tl;dr

Character.toString( yourAsciiNumber ) 

使用 Character#toString,而不是char。像这样:

String result = Character.toString( yourAsciiNumber ) ;

示例:

Character.toString( 97 )   // LATIN SMALL LETTER A

一个

Character.toString( 128_567 )   // FACE WITH MEDICAL MASK

tl;dr

Character.toString( yourAsciiNumber ) 

Use Character#toString, not char. Like this:

String result = Character.toString( yourAsciiNumber ) ;

Example:

Character.toString( 97 )   // LATIN SMALL LETTER A

a

Character.toString( 128_567 )   // FACE WITH MEDICAL MASK

????

char is legacy

The 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:

System.out.println( Character.toString( 128_567 ));  // Unicode code points handle full-range of Unicode characters.

????

This fails:

System.out.println( ( char ) 128_567 );  // `char` fails with most Unicode characters. 

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, call Character#toString.

String x = Character.toString( 97 ) ;

a

See this code run live at IdeOne.com.

旧人 2024-10-29 18:20:50

事实上在最后一个答案中
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'

随遇而安 2024-10-29 18:20:50

在 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));
}

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:

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];
}

Or, equivalently:

private static int ASCII_ZERO = 0x30;

private static char getDigit(int digitValue) {
  assertInRange(digitValue, 0, 9);
  return ((char) (digitValue + ASCII_ZERO));
}
冷夜 2024-10-29 18:20:50

最简单的方法是使用类型转换:

public char toChar(int c) {
    return (char)c;
}

The most simple way is using type casting:

public char toChar(int c) {
    return (char)c;
}
碍人泪离人颜 2024-10-29 18:20:50

最简单的方法是获取整数并使用强制转换运算符
前任

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);

The most simple way is to get integer and just use the casting operator
Ex

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文