Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hex conversion methods

Status
Not open for further replies.

dendenners

Programmer
Jul 17, 2001
110
IE
Hi there,
I'm having trouble converting a 4 byte hex value into the correct int value that it represents. I was wondering would anyone know where I could find some generic method that takes in a byte array with numerical hex values and returns the calculated int value? Thanks
(e.g. if my 4 byte array contains byte[0]=00,byte[1]=00, byte[2]=01, byte[3]=4A, the correct value is A*1 + 4*16 + 1*256=10 + 64 + 256 = 330)
 
You may wish to check out deitel & deitel "Java How to program" edition 3 page 491.

They have a little swing applicatioon which converts between number bases by entering the number and the radix (i.e. hex is 16, binary is 2 blah blah blah).

You can use some of the static methods available within the java.lang package such as Character.digit(yourChar, radix);

which will convert a character to its numerical equivalent depending on the value of the radix you pass.

e.g (A , 16) will return the value of 10

you can check out their books (all code examples are downloadable from their website) at the following url, I couldn't find the third edition so you may have to download the code examples and look for the chapter on Strings and Characters (chapter 10) for the example I am looking at.

best of luck [pipe]

 
An example of what you are trying to achieve:
Code:
import java.io.ByteArrayInputStream;

public class IntStream extends ByteArrayInputStream {

  public IntStream( byte[] buf ) {
    super( buf );
  }

  public int readInt() {
    int result = 0;
    int current;
    for( int i = 0; i < 4; i++ ) {
      if( ( current = this.read() ) == -1 )
        return -1;
      result = result * 256 + current;
    }
    return result;
  }


  public static void main( String[] args ) {
    IntStream ints = new IntStream( new byte[] { 0x00, 0x00, 0x01, 0x4A } );
    int current;
    while( ( current = ints.readInt() ) != -1 )
      System.out.println( current );
  }
}
Sorry if I'm missing a more obvious conversion of real byte quartets to integers... :)
Cheers, Neil



 
Or another way:
Code:
public class Convert {

  private Convert() {}

  public static int convert( byte[] buf ) {
    if( buf.length > 4 )
      throw new IllegalArgumentException( &quot;array length must not exceed 4&quot;);

    return ( (int) buf[0] << 24 ) +
           ( (int) buf[1] << 16 ) +
           ( (int) buf[2] << 8  ) + 
           ( (int) buf[3] );
  }

  public static void main( String[] args ) {
    System.out.println( convert( new byte[] { 0x00, 0x00, 0x01, 0x4A } ) );
  }
}
:)
 
Figured out the best approach:
Code:
import java.io.DataInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class BestMethod {

  private BestMethod() {}

  public static int convert( byte[] buf )
    throws IOException
  {
    if( buf.length > 4 )
      throw new IllegalArgumentException( &quot;array length must not exceed 4&quot;);

    DataInputStream dis = new DataInputStream( new ByteArrayInputStream( buf ) );

    return dis.readInt();
  }

  public static void main( String[] args ) {
    try {
      System.out.println( convert( new byte[] { 0x00, 0x00, 0x01, 0x4A } ) );
    } catch( IOException e ) {}
  }
}
Thanks for posting the question dendenners. Its helped me learn a bit more about the java.io package :)
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top