在java中将字符转换为ASCII数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16458564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:01:09  来源:igfitidea点击:

Convert character to ASCII numeric value in java

javastringascii

提问by Celta

I have String name = "admin";
then I do String char = name.substring(0,1); //char="a"

我有String name = "admin";
然后我做String char = name.substring(0,1); //char="a"

I want to convert the charto its ASCII value (97), how can I do this in java?

我想将 转换char为它的 ASCII 值 (97),我该如何在 java 中做到这一点?

采纳答案by SudoRahul

Very simple. Just cast your charas an int.

很简单。只需将您char作为int.

char character = 'a';    
int ascii = (int) character;

In your case, you need to get the specific Character from the String first and then cast it.

在您的情况下,您需要先从字符串中获取特定的字符,然后再进行转换。

char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.

Though cast is not required explicitly, but its improves readability.

虽然没有明确要求强制转换,但它提高了可读性。

int ascii = character; // Even this will do the trick.

回答by Daniel Lerps

Just cast the char to an int.

只需将字符转换为整数。

char character = 'a';
int number = (int) character;

The value of numberwill be 97.

的值为number97。

回答by christopher

Instead of this:

取而代之的是:

String char = name.substring(0,1); //char="a"

You should use the charAt()method.

您应该使用该charAt()方法。

char c = name.charAt(0); // c='a'
int ascii = (int)c;

回答by Vineet Singla

Convert the char to int.

将字符转换为整数。

    String name = "admin";
    int ascii = name.toCharArray()[0];

Also :

还 :

int ascii = name.charAt(0);

回答by Ricardo Cacheira

It's simple, get the character you want, and convert it to int.

很简单,得到你想要的字符,然后转换成int。

String name = "admin";
int ascii = name.charAt(0);

回答by user1721904

If you wanted to convert the entire string into concatenated ASCII values then you can use this -

如果您想将整个字符串转换为连接的 ASCII 值,那么您可以使用这个 -

    String str = "abc";  // or anything else

    StringBuilder sb = new StringBuilder();
    for (char c : str.toCharArray())
    sb.append((int)c);

    BigInteger mInt = new BigInteger(sb.toString());
    System.out.println(mInt);

wherein you will get 979899 as output.

其中您将获得 979899 作为输出。

Credit to this.

归功于

I just copied it here so that it would be convenient for others.

我只是把它复制到这里,以便其他人方便。

回答by stinepike

just a different approach

只是一种不同的方法

    String s = "admin";
    byte[] bytes = s.getBytes("US-ASCII");

bytes[0]will represent ascii of a.. and thus the other characters in the whole array.

bytes[0]将代表 a.. 的 ascii,从而代表整个数组中的其他字符。

回答by Sandeepp Sah

String str = "abc";  // or anything else

// Stores strings of integer representations in sequence
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
    sb.append((int)c);

 // store ascii integer string array in large integer
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);

回答by Sandeepp Sah

I know this has already been answered in several forms but here is my bit of code with a look to go through all the characters.

我知道这已经以多种形式得到了回答,但这是我的一些代码,用于查看所有字符。

Here is the code, started with the class

这是代码,从类开始

public class CheckChValue {  // Class name
public static void main(String[] args) { // class main

    String name = "admin"; // String to check it's value
    int nameLenght = name.length(); // length of the string used for the loop

    for(int i = 0; i < nameLenght ; i++){   // while counting characters if less than the length add one        
        char character = name.charAt(i); // start on the first character
        int ascii = (int) character; //convert the first character
        System.out.println(character+" = "+ ascii); // print the character and it's value in ascii
    }
}

}

}

回答by Raúl Vela

You can check the ASCII′s number with this code.

您可以使用此代码检查 ASCII 的编号。

String name = "admin";
char a1 = a.charAt(0);
int a2 = a1;
System.out.println("The number is : "+a2); // the value is 97

If I am wrong, apologies.

如果我错了,道歉。