The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Converting a text string to hex numvers

Posted by Kishori Sharan on January 08, 2001 at 9:40 AM

We always have an array of numbers like int. You cannot declare that it is an array of hex no. Whenevr, you need that no to be represendted in hex as a string you can use
String temp = Interger.toHexString ( your_no ) ;
So you can convert the string which holds many numbers in hex format to an array of int as follows.
String hex_string ;

// Since you know the no. of chars in string is always even then
int len = hex_string.length / 2 ;
int[] all_no = new int[len] ;

// Start a loop which reads two chars at a time in a string
for ( int i = 0 ; i < len ; i++ ) {
try {
// read two chars a time in two_chars string variable
all_no[i] = Integer.parseInt ( two_chars, 16 ) ;
catch ( NumberFormatException e ) {



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us