[Java 기초 테마] 인코딩과 인코딩(06) - 문자의 각종 값 변환
4436 단어 java 기초
package example.encoding;
/** *//**
* The Class ChineseValidator.
*/
public class CharacterValueConveter {
/** *//**
* The main method.
*
* @param args the arguments
*/
public static void main(String args[]) {
CharacterValueConveter conveter = new CharacterValueConveter();
conveter.testConvertion(" ");
}
/** *//**
* Test convertion.
*
* @param content the content
*/
public void testConvertion(String content) {
byte[] bytes = content.getBytes();
System.out.println("Original String : " + content);
System.out.println();
// Get bytes array
System.out.println("Convert string to bytes array : ");
for (int i = 0; i < bytes.length; i++) {
System.out.println(" byte[" + i + "]=" + bytes[i]);
}
System.out.println();
// Get each character and unicode
System.out.println("Start to convert by character: " + "
");
for (int j = 0; j < content.length(); j++) {
char c = content.charAt(j);
printAllValue(c);
}
}
/** *//**
* Prints the all value.
*
* @param c the c
*/
public void printAllValue(char c) {
byte b = (byte) c;
int i = (int) c;
short s = (short) c;
String binaryString = Integer.toBinaryString(i);
String hexString = Integer.toHexString(i);
StringBuffer output = new StringBuffer();
// Base value
output.append("Character : ").append(c).append("
");
output.append(" byte value : ").append(b).append("
");
output.append(" int value : ").append(i).append("
");
output.append(" short value : ").append(s).append("
");
// Hex value and binary value
output.append(" hex value :").append(hexString).append("
");
output.append(" binary value : ").append(binaryString).append("
");
// Unicode value in java file & web page with decimal encoding
output.append(" unicode value (Base 10): ");
output.append("\\u").append(i).append(";").append("
");
output.append(" unicode value (Base 10 in web page): ");
output.append("&#").append(i).append(";").append("
");
// Unicode value in java file & web page with hexadecimal encoding
output.append(" unicode value (Base 16): ");
output.append("\\u").append(hexString).append(";").append("
");
output.append(" unicode value (Base 16 in web page): ");
output.append("&#").append(hexString).append(";").append("
");
System.out.println(output.toString());
}
}
최종 테스트 결과는 다음과 같습니다.
Original String: 중국어
Convert string to bytes array : byte[0]=-42 byte[1]=-48 byte[2]=-50 byte[3]=-60
Start to convert by character:
Character: 중간byte value: 45 int value: 20013 short value: 20013 hex value: 4e2d binary value: 10011100010110 unicode value(Base 10):\u20013; unicode value (Base 10 in web page): 中 unicode value (Base 16):\u4e2d; unicode value (Base 16 in web page): e2d;
Character: Byte value: -121 int value: 25991 short value: 25991 hex value: 6587 binary value: 1100110000111 unicode value(Base 10):\u25991; unicode value (Base 10 in web page): 文 unicode value (Base 16):\u6587; unicode value (Base 16 in web page): ᦻ자바에서 문자의 유니버설은 두 가지 표시가 있는데 하나는 10진수 형식이고 하나는 16진수 형식이다.그것들은 각각 int i = (int)(string.charAt(i))와 Integer를 통과할 수 있다.toHexString(i);획득하다.그리고 자바 파일과 웹 페이지에서 같은 유니버설의 표현 형식은 다르다.웹 페이지는 &#로 이스케이프해야 하고 자바 파일에서는\u로 이스케이프해야 합니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 네트워크 프로그래밍의 UDP 서버 및 클라이언트 프로그램서버: 클라이언트: UDP: 클라이언트를 열어 데이터를 받을 때까지 기다린 다음 서버를 열어 데이터를 보냅니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.