Java 如何将十六进制值转换为特殊字符(Unicode)?
在我的文章中,我问了特殊字符到十六进制的转换 的十六进制值ㅂ"是“e38582” 现在我得到了字符串中的十六进制值 字符串hex=“e38582” 如何转换此十六进制值以获得特殊字符。(在本例中为)ㅂ“) 我已尝试此操作,但获取非法FormatConversionException:Java 如何将十六进制值转换为特殊字符(Unicode)?,java,android,string,unicode,utf-8,Java,Android,String,Unicode,Utf 8,在我的文章中,我问了特殊字符到十六进制的转换 的十六进制值ㅂ"是“e38582” 现在我得到了字符串中的十六进制值 字符串hex=“e38582” 如何转换此十六进制值以获得特殊字符。(在本例中为)ㅂ“) 我已尝试此操作,但获取非法FormatConversionException: String hex = "e38582"; BigInteger value = new BigInteger(hex, 16); String str = String.format(&qu
String hex = "e38582";
BigInteger value = new BigInteger(hex, 16);
String str = String.format("%c", value );
System.out.println("String : "+ str);
请从以下位置尝试此方法:
公共字符串转换器hextostring(字符串十六进制){
StringBuilder sb=新的StringBuilder();
StringBuilder temp=新的StringBuilder();
//49204c6f7665204a617661拆分为两个字符49、20、4c。。。
对于(int i=0;i请尝试以下方法:
公共字符串转换器hextostring(字符串十六进制){
StringBuilder sb=新的StringBuilder();
StringBuilder temp=新的StringBuilder();
//49204c6f7665204a617661拆分为两个字符49、20、4c。。。
对于(int i=0;i
将十六进制转换为字节数组
使用正确的编码解释字节数组(基于前面的问题UTF-8):
将十六进制转换为字节数组
使用正确的编码解释字节数组(基于前面的问题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);
}
公共静态字符串转换器hextounicode(字符串十六进制值)
{
hex\u value=“1f64c\u 1f3fb”//要尝试的示例值。。。
String[]数组_hex_strings=TextUtils.split(十六进制值“);
字符串final_unicode_value=“”;
//------------------------------------------------------------------------------------------
for(int i=0;i>>8);
字节lo=(字节)(c&0xff);
返回byteToHex(高)+byteToHex(低);
}
这是我写的一些代码,它将把十六进制字符串转换成Java utf16代码单位。然后可以将这些代码单位输入到Android中的web浏览器或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);
}
公共静态字符串转换器hextounicode(字符串十六进制值)
{
hex\u value=“1f64c\u 1f3fb”//要尝试的示例值。。。
String[]数组_hex_strings=TextUtils.split(十六进制值“);
字符串final_unicode_value=“”;
//------------------------------------------------------------------------------------------
for(int i=0;ipublic 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()