Java 如何将十六进制值转换为特殊字符(Unicode)?
在我的文章中,我问了特殊字符到十六进制的转换 十六进制值ㅂ 是e38582吗 现在我得到了字符串中的十六进制值 字符串十六进制=e38582 如何转换此十六进制值以获得特殊字符。在这个例子中,它是ㅂ 我已尝试此操作,但获取非法FormatConversionException:Java 如何将十六进制值转换为特殊字符(Unicode)?,java,android,string,unicode,utf-8,Java,Android,String,Unicode,Utf 8,在我的文章中,我问了特殊字符到十六进制的转换 十六进制值ㅂ 是e38582吗 现在我得到了字符串中的十六进制值 字符串十六进制=e38582 如何转换此十六进制值以获得特殊字符。在这个例子中,它是ㅂ 我已尝试此操作,但获取非法FormatConversionException: String hex = "e38582"; BigInteger value = new BigInteger(hex, 16); String str = String.format("
String hex = "e38582";
BigInteger value = new BigInteger(hex, 16);
String str = String.format("%c", value );
System.out.println("String : "+ str);
请从以下位置尝试此方法:
}
将十六进制转换为字节数组
根据前面的问题UTF-8,使用正确的编码解释字节数组:
String output = new String(bytes, "UTF-8");
这是我写的一些代码,它将把十六进制字符串转换成JavaUTF16代码单元。这些代码单元可以输入到你的网络浏览器或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);
}
谢谢你,你的帖子包含了我想要的一些代码。我认为你的回答比公认的答案更有帮助。
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()