简体   繁体   English

Java - 将 int 更改为 ascii

[英]Java - Change int to ascii

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

Do you want to convert int s to char s?:要将int转换为char吗?:

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

Or do you want to convert int s to String s?或者你想将int s 转换为String s?

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

Or what is it that you mean?或者你的意思是什么?

If you first convert the int to a char, you will have your ascii code.如果您首先将 int 转换为 char,您将拥有您的 ascii 代码。

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

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:有很多方法可以将 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();
}

For example, the ASCII text for "TEST" can be represented as the byte array:例如,“TEST”的 ASCII 文本可以表示为字节数组:

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). ...所以这实际上将 32 位整数中的 4 个字节转换为 4 个单独的 ASCII 字符(每个字节一个字符)。

You can convert a number to ASCII in java.您可以在 Java 中将数字转换为 ASCII。 example converting a number 1 (base is 10) to ASCII.将数字 1(基数为 10)转换为 ASCII 的示例。

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

Output:输出:

Character: 1
Character: 49

In fact in the last answer String strAsciiTab = Character.toString((char) iAsciiValue);事实上在最后一个答案 String strAsciiTab = Character.toString((char) iAsciiValue); the essential part is (char)iAsciiValue which is doing the job (Character.toString useless)必不可少的部分是 (char)iAsciiValue 正在完成这项工作(Character.toString 没用)

Meaning the first answer was correct actually char ch = (char) yourInt;这意味着第一个答案实际上是正确的 char ch = (char) yourInt;

if in yourint=49 (or 0x31), ch will be '1'如果在 yourint=49(或 0x31)中,ch 将是 '1'

In Java, you really want to use Integer.toString to convert an integer to its corresponding String value.在 Java 中,您确实希望使用Integer.toString将整数转换为其对应的 String 值。 If you are dealing with just the digits 0-9, then you could use something like this:如果你只处理数字 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];
}

Or, equivalently:或者,等效地:

private static int ASCII_ZERO = 0x30;

private static char getDigit(int digitValue) {
  assertInRange(digitValue, 0, 9);
  return ((char) (digitValue + ASCII_ZERO));
}

The most simple way is using type casting:最简单的方法是使用类型转换:

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

tl;dr tl;博士<\/h1>

Use Character#toString<\/code> , not char<\/code> .使用Character#toString<\/code> ,而不是char<\/code> 。

 String result = Character.toString( yourAsciiNumber ) ;<\/code><\/pre>

Ex:前任:

 Character.toString( 97 ) \/\/ LATIN SMALL LETTER A<\/code><\/pre> 
       

a一个

<\/blockquote>

Character.toString( 128_567 ) \/\/ FACE WITH MEDICAL MASK<\/code><\/pre> 
          

😷 😷

<\/blockquote>

char<\/code> is legacy char<\/code>是遗产<\/h1>

The char<\/code> type in Java is legacy, and is essentially broken. Java 中的char<\/code>类型是遗留的,本质上是被破坏的。 As a 16-bit<\/a> value, char<\/code> is incapable of representing most characters defined by Unicode<\/a> .作为16 位<\/a>值, char<\/code>无法表示Unicode<\/a>定义的大多数字符。

This succeeds:这成功了:

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

😷 😷

<\/blockquote>

This fails:这失败了:

 System.out.println( ( char ) 128_567 ); \/\/ `char` fails with most Unicode characters.<\/code><\/pre>

See code run live at IdeOne.com<\/a> .查看在 IdeOne.com 上实时运行的代码<\/a>。

Code point码点<\/h1>

Use code point<\/a> integer numbers to represent individual letters.使用代码点<\/a>整数来表示单个字母。

US-ASCII<\/a> is a subset of Unicode. US-ASCII<\/a>是 Unicode 的子集。 So, any US-ASCII number (0-127) is also a Unicode code point (0-1,114,111).因此,任何 US-ASCII 数字 (0-127) 也是一个 Unicode 代码点 (0-1,114,111)。

To change a code point number to a String<\/code> object containing a single character, call Character#toString<\/code><\/a> .要将代码点编号更改为包含单个字符的String<\/code>对象,请调用Character#toString<\/code><\/a> 。

 String x = Character.toString( 97 ) ;<\/code><\/pre> 
                         

a一个

<\/blockquote>

See this code run live at IdeOne.com<\/a> .请参阅在 IdeOne.com 上实时运行的代码<\/a>。

"

The most simple way is to get integer and just use the casting operator Ex最简单的方法是获取整数并仅使用强制转换运算符 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM