我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
当前回答
非常简单。只需将char类型转换为int类型。
char character = 'a';
int ascii = (int) character;
在本例中,您需要首先从String中获取特定的字符,然后强制转换它。
char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.
虽然没有明确要求强制转换,但它提高了可读性。
int ascii = character; // Even this will do the trick.
其他回答
非常简单。只需将char类型转换为int类型。
char character = 'a';
int ascii = (int) character;
在本例中,您需要首先从String中获取特定的字符,然后强制转换它。
char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.
虽然没有明确要求强制转换,但它提高了可读性。
int ascii = character; // Even this will do the trick.
只需将char类型转换为int类型。
char character = 'a';
int number = (int) character;
number的值为97。
而不是这样:
String char = name.substring(0,1); //char="a"
您应该使用charAt()方法。
char c = name.charAt(0); // c='a'
int ascii = (int)c;
一个简单的方法是:
int character = 'a';
如果你输入“character”,你得到97。
你可以用这段代码检查ASCII的数字。
String name = "admin";
char a1 = a.charAt(0);
int a2 = a1;
System.out.println("The number is : "+a2); // the value is 97
如果我错了,我道歉。