如何将ASCII码(0-255)转换为其对应的字符?

发布于 2024-12-08 18:38:10 字数 128 浏览 0 评论 0原文

在 Java 中,如何将 ASCII 代码([0, 255] 范围内的整数)转换为其相应的 ASCII 字符?

例如:

65  -> "A"
102 -> "f"

How can I convert, in Java, the ASCII code (which is an integer from [0, 255] range) to its corresponding ASCII character?

For example:

65  -> "A"
102 -> "f"

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

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

发布评论

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

评论(10

独留℉清风醉 2024-12-15 18:38:10

System.out.println((char)65);
会打印“A”

System.out.println((char)65);
would print "A"

自找没趣 2024-12-15 18:38:10

String.valueOf<代码>(Character.toChars(int)< /a>)

假设整数如您所说,在 0 到 255 之间,您将从 Character.toChars 返回一个包含单个字符的数组,这将成为一个传递给 String.valueOf 时为单字符字符串。

使用 Character.toChars 优于涉及从 intchar 转换的方法(即 (char) i)出于多种原因,包括如果您未能正确验证整数,则 Character.toChars 将抛出 IllegalArgumentException,而强制转换将吞掉错误(根据 缩小原始转换规范),可能会给出与您不同的输出故意的。

String.valueOf(Character.toChars(int))

Assuming the integer is, as you say, between 0 and 255, you'll get an array with a single character back from Character.toChars, which will become a single-character string when passed to String.valueOf.

Using Character.toChars is preferable to methods involving a cast from int to char (i.e. (char) i) for a number of reasons, including that Character.toChars will throw an IllegalArgumentException if you fail to properly validate the integer while the cast will swallow the error (per the narrowing primitive conversions specification), potentially giving an output other than what you intended.

娇俏 2024-12-15 18:38:10
int number = 65;
char c = (char)number;

这是一个简单的解决方案

int number = 65;
char c = (char)number;

it is a simple solution

聊慰 2024-12-15 18:38:10
    new String(new char[] { 65 })

您最终将得到一个长度为 1 的字符串,其单个字符的 (ASCII) 代码为 65。在 Java 中,字符是数字数据类型。

    new String(new char[] { 65 })

You will end up with a string of length one, whose single character has the (ASCII) code 65. In Java chars are numeric data types.

不气馁 2024-12-15 18:38:10

执行相同操作的更简单方法:

键入将整数转换为字符,让 int n 为整数,
然后:

Char c=(char)n;
System.out.print(c)//char c will store the converted value.

An easier way of doing the same:

Type cast integer to character, let int n be the integer,
then:

Char c=(char)n;
System.out.print(c)//char c will store the converted value.
烈酒灼喉 2024-12-15 18:38:10

可以像这样从 a 迭代到 z

int asciiForLowerA = 97;
int asciiForLowerZ = 122;
for(int asciiCode = asciiForLowerA; asciiCode <= asciiForLowerZ; asciiCode++){
    search(sCurrentLine, searchKey + Character.toString ((char) asciiCode));
}

One can iterate from a to z like this

int asciiForLowerA = 97;
int asciiForLowerZ = 122;
for(int asciiCode = asciiForLowerA; asciiCode <= asciiForLowerZ; asciiCode++){
    search(sCurrentLine, searchKey + Character.toString ((char) asciiCode));
}
寄居者 2024-12-15 18:38:10
    for (int i = 0; i < 256; i++) {
        System.out.println(i + " -> " + (char) i);
    }

    char lowercase = 'f';
    int offset = (int) 'a' - (int) 'A';
    char uppercase = (char) ((int) lowercase - offset);
    System.out.println("The uppercase letter is " + uppercase);

    String numberString = JOptionPane.showInputDialog(null,
            "Enter an ASCII code:",
            "ASCII conversion", JOptionPane.QUESTION_MESSAGE);

    int code = (int) numberString.charAt(0);
    System.out.println("The character for ASCII code "
            + code + " is " + (char) code);
    for (int i = 0; i < 256; i++) {
        System.out.println(i + " -> " + (char) i);
    }

    char lowercase = 'f';
    int offset = (int) 'a' - (int) 'A';
    char uppercase = (char) ((int) lowercase - offset);
    System.out.println("The uppercase letter is " + uppercase);

    String numberString = JOptionPane.showInputDialog(null,
            "Enter an ASCII code:",
            "ASCII conversion", JOptionPane.QUESTION_MESSAGE);

    int code = (int) numberString.charAt(0);
    System.out.println("The character for ASCII code "
            + code + " is " + (char) code);
清风无影 2024-12-15 18:38:10

这是一个例子,说明通过将int转换为char,可以确定ASCII码对应的字符。

public class sample6
{
    public static void main(String... asf)
    {

        for(int i =0; i<256; i++)
        {
            System.out.println( i + ". " + (char)i);
        }
    }
}

This is an example, which shows that by converting an int to char, one can determine the corresponding character to an ASCII code.

public class sample6
{
    public static void main(String... asf)
    {

        for(int i =0; i<256; i++)
        {
            System.out.println( i + ". " + (char)i);
        }
    }
}
花开半夏魅人心 2024-12-15 18:38:10

上面的答案仅接近解决问题。这是你的答案:

Integer.decode(Character.toString(char c));

upper answer only near solving the Problem. heres your answer:

Integer.decode(Character.toString(char c));

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