[英]How to convert hex value to Special Char( in Unicode )?
在我之前的問題中,我詢問了特殊字符到十六進制的轉換。
“ㅂ”的十六進制值為“e38582”
現在我在字符串中有十六進制值。
字符串十六進制 = "e38582";
如何轉換此十六進制值以獲取特殊字符。(在此示例中為"ㅂ" )
我試過這個,但得到 IllegalFormatConversionException :
String hex = "e38582";
BigInteger value = new BigInteger(hex, 16);
String str = String.format("%c", value );
System.out.println("String : "+ str);
從mkyong 的博客試試這個方法:
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
將十六進制轉換為字節數組。 使用 Java 將十六進制轉儲的字符串表示形式轉換為字節數組?
用正確的編碼解釋字節數組(基於您之前的問題,UTF-8):
String output = new String(bytes, "UTF-8");
這是我編寫的一些代碼,它會將您的十六進制字符串轉換為 Java utf16 代碼單元。 然后可以將代碼單元輸入到諸如您的 Web 瀏覽器或 Android 中的 EditText 視圖之類的東西中,並且將為您完成到字符的轉換。
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);
}
val hex = "f135"
val char = hex.toLong(16).toChar()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.