How to print byte array as hex values in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        byte[] bytes = {74, 97, 118, 97};  
        
        for (byte ch : bytes) {
            System.out.print(Integer.toHexString(ch) + " ");
        }
    }
}
 
 
 
 
/*
run:

4a 61 76 61 

*/

 



answered Oct 26, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        byte[] bytes = {74, 97, 118, 97};  
        
        for (byte ch : bytes) {
            System.out.print(String.format("%02X ", ch));
        }
    }
}
 
 
 
 
/*
run:

4A 61 76 61 

*/

 



answered Oct 26, 2023 by avibootz

Related questions

2 answers 190 views
1 answer 104 views
104 views asked Jan 3, 2025 by avibootz
4 answers 210 views
1 answer 438 views
1 answer 204 views
1 answer 187 views
187 views asked Jan 21, 2021 by avibootz
1 answer 146 views
...