【问题标题】:How to convert hex value to Special Char( in Unicode )?如何将十六进制值转换为特殊字符(在 Unicode 中)?
【发布时间】:2014-09-28 02:20:29
【问题描述】:

在我的previous question 中,我询问了将特殊字符转换为十六进制的问题。

"ㅂ"的十六进制值为"e38582"

现在我在 String 中得到了十六进制值。

字符串十六进制 = "e38582";

如何将此十六进制值转换为特殊字符。(在本例中为 "ㅂ"

我试过这个,但得到 IllegalFormatConversionException :

String hex = "e38582";
BigInteger value = new BigInteger(hex, 16);
String str = String.format("%c", value );
System.out.println("String : "+ str);

【问题讨论】:

    标签: java android string unicode utf-8


    【解决方案1】:

    mkyong's blog试试这个方法:

      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();
    

    }

    【讨论】:

    • 谢谢,您的帖子包含了我正在寻找的一些代码。我认为您的回答比接受的回答更有帮助。
    【解决方案2】:
    1. 将十六进制转换为字节数组。 Convert a string representation of a hex dump to a byte array using Java?

    2. 用正确的编码解释字节数组(基于你之前的问题,UTF-8):

      String output = new String(bytes, "UTF-8");
      

    【讨论】:

      【解决方案3】:

      这是我编写的一些代码,它将您的十六进制字符串转换为 Java utf16 代码单元。然后可以将代码单元输入到您的网络浏览器或 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);
      }
      

      【讨论】:

        【解决方案4】:
        val hex = "f135"
        val char = hex.toLong(16).toChar()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-08
          • 1970-01-01
          • 2013-10-09
          • 1970-01-01
          • 2018-01-22
          • 2016-08-07
          • 2014-12-22
          相关资源
          最近更新 更多