How to convert hex value to Special Char( in Unicode )?

后端 未结 4 1029
说谎
说谎 2020-12-22 14:30

In my previous question, I asked about conversion of Special Chars to Hex.

Hex value of "ㅂ" is "e38582"

4条回答
  •  情深已故
    2020-12-22 15:02

    Here is some code I've written, it will convert your hex string into Java utf16 code units. There code units can then be entered into something like your web browser or an EditText View in Android and the conversion to a Character will be done for you.

    public static String convertHexToUnicode(String hex_value)
    {
        hex_value = "1f64c_1f3fb";  //Example value to try...
        String[] array_hex_strings = TextUtils.split(hex_value, "_");
    
        String final_unicode_value = "";
        //------------------------------------------------------------------------------------------
        for (int i=0; i < array_hex_strings.length; i++)
        {
            Log.i("People", "method(); array_hex_strings[i]: " + array_hex_strings[i]);
    
            int decimal = Integer.parseInt(array_hex_strings[i], 16);
            Log.i("People", "method(); decimal: " + decimal);
            Log.i("People", "method(); Integer.toHexString(decimal): " + Integer.toHexString(decimal));
            Log.i("People", "method(); Character.isValidCodePoint(decimal): " + Character.isValidCodePoint(decimal));
    
            char[] codepoint_char_array = Character.toChars(decimal);
    
            String combined_utf16_code_units = charArrayToUtf16CodeUnits(codepoint_char_array);
    
            final_unicode_value = (final_unicode_value + combined_utf16_code_units);
            Log.i("People", "unicode_value_evolving: " + final_unicode_value);
        }
        //------------------------------------------------------------------------------------------
        Log.i("People", "final_unicode_value: " + final_unicode_value);
    
        return final_unicode_value;
    }
    
    public static String charArrayToUtf16CodeUnits(char[] char_array)
    {
        /*Utf16 code units are also known as Java Unicode*/
        System.out.println("People; array.length: = " + char_array.length);
    
        String full_unicode_value = "";
        //------------------------------------------------------------------------------------------
        for (int i = 0; i < char_array.length; i++)
        {
            System.out.println("array char: " + "[" + i + "]" + " converted to hex" + " = " + charToHex(char_array[i]));
    
            full_unicode_value = (full_unicode_value + "\\" + "u" + charToHex(char_array[i]));
        }
        //------------------------------------------------------------------------------------------
        Log.i("People", "full_unicode_value: " + full_unicode_value);
    
        return full_unicode_value;
    }
    
    static public String charToHex(char c)
    {
        //Returns hex String representation of char c
        byte hi = (byte) (c >>> 8);
        byte lo = (byte) (c & 0xff);
    
        return byteToHex(hi) + byteToHex(lo);
    }
    

提交回复
热议问题