Converting A String To Hexadecimal In Java

前端 未结 21 2540
青春惊慌失措
青春惊慌失措 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:19

    A short and convenient way to convert a String to its Hexadecimal notation is:

    public static void main(String... args){
    String str = "Hello! This is test string.";
    char ch[] = str.toCharArray();
    StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ch.length; i++) {
            sb.append(Integer.toHexString((int) ch[i]));
        }
        System.out.println(sb.toString());
    }
    

提交回复
热议问题