Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to convert a hex string to a byte array in Java

4 Answers

0 votes
import java.util.Formatter;

public class Program {
    public static void printByteArray(byte[] barray) {
        StringBuilder sb = new StringBuilder("byte[] = ");
        
        Formatter formatter = new Formatter(sb);
        for (byte b : barray) {
            formatter.format("%02X ", b);
        }
        
        System.out.println(sb.toString());
    }

    public static byte[] hexStringToByteArray(String hex) {
        if (hex.length() % 2 != 0) {
            hex = "0" + hex; // Pad with leading zero if necessary
        }
        
        int length = hex.length();
        byte[] barr = new byte[length / 2];
        
        for (int i = 0; i < length; i += 2) {
            barr[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
        }
        
        return barr;
    }

    public static void main(String[] args) {
        byte[] barr = hexStringToByteArray("1B6E2AC");
        
        printByteArray(barr);
    }
}

  
  
/*
run:
  
byte[] = 01 B6 E2 AC 
  
*/

 



answered Jun 20, 2024 by avibootz
0 votes
import java.util.Formatter;

public class Program {

    static void printByteArray(byte[] barray) {
        StringBuilder sb = new StringBuilder("byte[] = ");
         
        Formatter formatter = new Formatter(sb);
        for (byte b : barray) {
            formatter.format("%02X ", b);
        }
         
        System.out.println(sb.toString());
    }
    
    public static byte[] hexStringToByteArray(String hex) {
        if (hex.length() % 2 != 0) {
            hex = "0" + hex; // Pad with leading zero if necessary
        }
        
        int len = hex.length();
        byte[] barr = new byte[len / 2];
        
        for (int i = 0; i < len; i += 2) {
            barr[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                                 + Character.digit(hex.charAt(i+1), 16));
        }
        
        return barr;
    }

    public static void main(String[] args) {
        byte[] barr = hexStringToByteArray("1B6E2AC");
        
        printByteArray(barr);
    }
}


  
  
/*
run:
  
byte[] = 01 B6 E2 AC 
  
*/

 



answered Jun 20, 2024 by avibootz
0 votes
import java.util.Formatter;
import java.util.ArrayList;
import java.util.List;

public class Program {

    static void printByteArray(byte[] barray) {
        StringBuilder sb = new StringBuilder("byte[] = ");
         
        Formatter formatter = new Formatter(sb);
        for (byte b : barray) {
            formatter.format("%02X ", b);
        }
         
        System.out.println(sb.toString());
    }
    
    public static byte[] hexStringToByteArray(String hex) {
        hex = hex.length() % 2 == 1 ? "0" + hex : hex; // Pad with leading zero if necessary
        
        List<Byte> byteList = new ArrayList<>();
        for (int i = 0; i < hex.length(); i += 2) {
            int value = Integer.parseInt(hex.substring(i, i + 2), 16);
            byteList.add((byte) value);
        }
        
        byte[] byteArray = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            byteArray[i] = byteList.get(i);
        }
        
        return byteArray;
    }

    public static void main(String[] args) {
        byte[] barr = hexStringToByteArray("1B6E2AC");
        
        printByteArray(barr);
    }
}


  
  
/*
run:
  
byte[] = 01 B6 E2 AC 
  
*/

 



answered Jun 20, 2024 by avibootz
0 votes
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String hexString = "12E3A4C"; 
        byte[] byteArray = hexStringToByteArray(hexString);

        System.out.println(Arrays.toString(byteArray));
    }

    public static byte[] hexStringToByteArray(String hex) {
        int len = hex.length();
        
        if (len % 2 != 0) {
            hex = "0" + hex;
            len++;
        }

        
        byte[] data = new byte[len / 2];


        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                                  + Character.digit(hex.charAt(i+1), 16));
        }
        
        return data;
    }
}



/*
run:

[1, 46, 58, 76]

*/

 



answered Feb 14, 2025 by avibootz
edited Feb 14, 2025 by avibootz

Related questions

2 answers 165 views
2 answers 169 views
1 answer 77 views
1 answer 70 views
...