How to convert byte array to hexadecimal string in Java

2 Answers

0 votes
public class ByteArrayToHexString {
    public static void main(String args[]) {
        byte[] bytes = {3, 10, 7, 15, 12};

        String hex = "";
        for (byte b : bytes) {
            hex += String.format("%02X", b);
        }

        System.out.print(hex);
    }
}


 
/*
run:
    
030A070F0C
  
*/

 



answered Jan 16, 2022 by avibootz
edited Jun 21, 2025 by avibootz
0 votes
public class ByteArrayToHexString {
    public static void main(String[] args) {
        String s = "JAVA C C++ C#";
                
        byte[] bytes = s.getBytes();
 
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        
        System.out.println(sb.toString());
    }
}


 
/*
run:
 
4A 41 56 41 20 43 20 43 2B 2B 20 43 23 
 
*/

 



answered Jun 21, 2025 by avibootz

Related questions

3 answers 266 views
266 views asked Oct 28, 2016 by avibootz
8 answers 844 views
1 answer 253 views
2 answers 167 views
1 answer 205 views
...