tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Uncommons Maths > Hex String to Byte Array

Hex String to Byte Array 

Uncommons Maths is a java library with nice math utilities and random number generators. This requires the library uncommons-maths-1.2.2.jar to be in classpath. The following example shows using BinaryUtils.convertHexStringToBytes() API.

File Name  :  
com/bethecoder/tutorials/uncommons/HexString2BytesTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.uncommons;

import java.nio.ByteBuffer;
import java.util.Arrays;

import org.uncommons.maths.binary.BinaryUtils;

public class HexString2BytesTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    String hexStr = "0107D9D1";
    
    //Convert hex string to byte array
    byte [] bytes =  BinaryUtils.convertHexStringToBytes(hexStr);
    System.out.println(Arrays.toString(bytes));
    
    //Convert byte array to number
    System.out.println(ByteBuffer.wrap(bytes).getInt());
  }

}
   

It gives the following output,
[1, 7, -39, -47]
17291729



 
  


  
bl  br