[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로 이스케이프해야 합니다.

좋은 웹페이지 즐겨찾기