根据你的要求,应该仅对ASCII有效,使用String.getBytes()方法就可以了,同时指定一个正确的字符集。 byte[] getBytes(String charsetName) Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
public class AAA{ public static void main(String args[]){ String s = "B"; System.out.println( s.getBytes()[0] );
public class Test{ public static void main(String []argv){ String str = "hello!"; for(int i = 0; i < str.length(); i++) System.out.println((int)str.charAt(i)); } }
char c='a'; int i=c; System.out.println("c的ASCII值为:"+i);int i=97; char c=(int)97; System.out.println("i的字符值为:"+c);
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
public static void main(String args[]){
String s = "B";
System.out.println( s.getBytes()[0] );
byte[] bs = { 66 };
System.out.println( new String(bs) );
}
}
public static void main(String []argv){
String str = "hello!";
for(int i = 0; i < str.length(); i++)
System.out.println((int)str.charAt(i));
}
}
int i=c;
System.out.println("c的ASCII值为:"+i);int i=97;
char c=(int)97;
System.out.println("i的字符值为:"+c);