Converting A String To Hexadecimal In Java

前端 未结 21 2215
青春惊慌失措
青春惊慌失措 2020-11-22 09:55

I am trying to convert a string like \"testing123\" into hexadecimal form in java. I am currently using BlueJ.

And to convert it back, is it the same thing except b

相关标签:
21条回答
  • 2020-11-22 10:35

    Here an other solution

    public static String toHexString(byte[] ba) {
        StringBuilder str = new StringBuilder();
        for(int i = 0; i < ba.length; i++)
            str.append(String.format("%x", ba[i]));
        return str.toString();
    }
    
    public static String fromHexString(String hex) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < hex.length(); i+=2) {
            str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
        }
        return str.toString();
    }
    
    0 讨论(0)
  • 2020-11-22 10:35

    Convert a letter in hex code and hex code in letter.

            String letter = "a";
        String code;
        int decimal;
    
        code = Integer.toHexString(letter.charAt(0));
        decimal = Integer.parseInt(code, 16);
    
        System.out.println("Hex code to " + letter + " = " + code);
        System.out.println("Char to " + code + " = " + (char) decimal);
    
    0 讨论(0)
  • 2020-11-22 10:37
    import org.apache.commons.codec.binary.Hex;
    ...
    
    String hexString = Hex.encodeHexString(myString.getBytes(/* charset */));
    

    http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html

    0 讨论(0)
  • 2020-11-22 10:40
    byte[] bytes = string.getBytes(CHARSET); // you didn't say what charset you wanted
    BigInteger bigInt = new BigInteger(bytes);
    String hexString = bigInt.toString(16); // 16 is the radix
    

    You could return hexString at this point, with the caveat that leading null-chars will be stripped, and the result will have an odd length if the first byte is less than 16. If you need to handle those cases, you can add some extra code to pad with 0s:

    StringBuilder sb = new StringBuilder();
    while ((sb.length() + hexString.length()) < (2 * bytes.length)) {
      sb.append("0");
    }
    sb.append(hexString);
    return sb.toString();
    
    0 讨论(0)
  • 2020-11-22 10:41

    Here's a short way to convert it to hex:

    public String toHex(String arg) {
        return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
    }
    
    0 讨论(0)
  • 2020-11-22 10:41

    The numbers that you encode into hexadecimal must represent some encoding of the characters, such as UTF-8. So first convert the String to a byte[] representing the string in that encoding, then convert each byte to hexadecimal.

    public static String hexadecimal(String input, String charsetName) throws UnsupportedEncodingException {
        if (input == null) throw new NullPointerException();
        return asHex(input.getBytes(charsetName));
    }
    
    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
    
    public static String asHex(byte[] buf)
    {
        char[] chars = new char[2 * buf.length];
        for (int i = 0; i < buf.length; ++i)
        {
            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
        }
        return new String(chars);
    }
    
    0 讨论(0)
提交回复
热议问题