누군가가 문자열 즉, 해당 ASCII 코드로 인쇄하는 방법을 말해 줄 수 있습니까?!인쇄 문자열을 바이트로
내 입력은 「9」등 정상적인 문자열로 출력 문자의 ASCII 값에 대응되어야 '9'
누군가가 문자열 즉, 해당 ASCII 코드로 인쇄하는 방법을 말해 줄 수 있습니까?!인쇄 문자열을 바이트로
내 입력은 「9」등 정상적인 문자열로 출력 문자의 ASCII 값에 대응되어야 '9'
String.getBytes()
사용 방법. 내가 아니라고
String s = "Some string here";
for (int i=0; i<s.length();i++)
System.out.println("ASCII value of: "+s.charAt(i) + " is:"+ (int)s.charAt(i));
이 질문을 참조 무엇을 원하지만 다음 방법으로 인쇄 할 수 있습니다.
String str = "9";
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i) + " ASCII " + (int) str.charAt(i));
}
당신은 순진한 접근 방식이 될 것 http://www.java-forums.org/java-tips/5591-printing-ascii-values-characters.html
안녕 : How to convert a Java String to an ASCII byte array?
가 아스키 당신이 할 수있는 각 문자의 값을 얻으려면 : - 당신은 바이트 배열을 찾고 있다면
byte []bytes="Hello".getBytes();
for(byte b:bytes)
System.out.println(b);
에서 그것을 볼 수 있습니다 :
:
final byte[] bytes = "FooBar".getBytes(); for (byte b : bytes) { System.out.print(b + " "); }
결과 : 70 111 111 66 97 114
또는 문자 배열을 통해 원시적 INT
for (final char c : "FooBar".toCharArray()) { System.out.print((int) c + " "); }
결과로 숯 변환 : 70 111 111 66 97 114
또는 Java8 덕분에 forEach를 통해 InputSteam을 통해 : "FooBar".chars().forEach(c -> System.out.print(c + " "));
결과 : 70 111 111 66 97 114
또는 Java8 덕분 및 Apache Commons Lang : final List<Byte> list = Arrays.asList(ArrayUtils.toObject("FooBar".getBytes())); list.forEach(b -> System.out.print(b + " "));
결과 : 70 111 111 66 97 114
더 나은 방법은 0을 사용하는 것입니다.(ASCII, UTF-8, ...)
// Convert a String to byte array (byte[])
final String str = "FooBar";
final byte[] arrayUtf8 = str.getBytes("UTF-8");
for(final byte b: arrayUtf8){
System.out.println(b + " ");
}
결과 : 70 111 111 66 97 114
final byte[] arrayUtf16 = str.getBytes("UTF-16BE");
for(final byte b: arrayUtf16){
System.out.println(b);
}
결과 : 70 0 111 0 111 0 66 0 97 0 114
희망 도움이되었습니다.
ASCII에서는 훌륭하게 작동하지만 8 비트 반으로 실행하면 음수가됩니다. 자바에서 바이트로 결정된 힘이 서명되기 때문입니다. – Thilo