Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How: convert byte[] to hex encoded string

Reply
Thread Tools

How: convert byte[] to hex encoded string

 
 
gauravs_mailbox@yahoo.com
Guest
Posts: n/a
 
      09-25-2006
Hi All,

I want to covert the byte to hex encoded string.

Any pointers how to do that , would be really appreciated.

Regards,
Gaurav

 
Reply With Quote
 
 
 
 
Thomas Kellerer
Guest
Posts: n/a
 
      09-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote on 25.09.2006 13:20:
> Hi All,
>
> I want to covert the byte to hex encoded string.
>
> Any pointers how to do that , would be really appreciated.


Loop over the bytes and use Integer.toHexString()

Thomas

 
Reply With Quote
 
 
 
 
gauravs_mailbox@yahoo.com
Guest
Posts: n/a
 
      09-25-2006

Thomas Kellerer wrote:
> (E-Mail Removed) wrote on 25.09.2006 13:20:
> > Hi All,
> >
> > I want to covert the byte to hex encoded string.
> >
> > Any pointers how to do that , would be really appreciated.

>
> Loop over the bytes and use Integer.toHexString()
>
> Thomas


Hi Thomas,

thnx for your reply..

But I want to make sure that my string is exactly 20 bytes long and if
its not , i need to pad it up .. any tips ?

 
Reply With Quote
 
Thomas Kellerer
Guest
Posts: n/a
 
      09-25-2006
(E-Mail Removed) wrote on 25.09.2006 14:46:
> Thomas Kellerer wrote:
>> (E-Mail Removed) wrote on 25.09.2006 13:20:
>>> Hi All,
>>>
>>> I want to covert the byte to hex encoded string.
>>>
>>> Any pointers how to do that , would be really appreciated.

>> Loop over the bytes and use Integer.toHexString()
>>
>> Thomas

>
> Hi Thomas,
>
> thnx for your reply..
>
> But I want to make sure that my string is exactly 20 bytes long and if
> its not , i need to pad it up .. any tips ?
>

Then you will need to check the length of your byte array and the length of the
resulting string. Where is the problem?
 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      09-25-2006
(E-Mail Removed) wrote:
> I want to convert the byte array to hex encoded string.
>
> Any pointers how to do that , would be really appreciated.


<http://www.websina.com/products/license/com/websina/util/ByteHex.html>
 
Reply With Quote
 
Red Orchid
Guest
Posts: n/a
 
      09-25-2006
(E-Mail Removed) wrote or quoted in
Message-ID: <(E-Mail Removed) om>:

>
> I want to covert the byte to hex encoded string.
>



This is one example.

<code>
// not tested.

protected static final byte[] Hexhars = {

'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};


public static String encode(byte[] b) {

StringBuilder s = new StringBuilder(2 * b.length);

for (int i = 0; i < b.length; i++) {

int v = b[i] & 0xff;

s.append((char)Hexhars[v >> 4]);
s.append((char)Hexhars[v & 0xf]);
}

return s.toString();
}

</code>


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) wrote:
> Hi All,
>
> I want to covert the byte to hex encoded string.
>
> Any pointers how to do that , would be really appreciated.
>
> Regards,
> Gaurav
>


Another thought:

static void loadFile (JFrameFileLoader jfl, File f)
{
JTextArea jta = jfl.getTextArea();

if (f != null)
{
jfl.setFilename( f.getName () );
jta.setText ( "" );
FileChannel fc;
try
{
fc = new FileInputStream (f).getChannel();
} catch (FileNotFoundException ex)
{
jta.setText ("File not found.");
return;
}

MappedByteBuffer mmf;
try
{
mmf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
} catch (IOException ex)
{
jta.setText ("Error opening file.");
return;
}
try
{
for (long i=0; i < fc.size(); i+=16)
{
jta.append ( myToHexString(i, );
jta.append ( ": " );
long lastBytes = fc.size () - i;
for (long j=0; j < 16 && j < lastBytes; j++)
{
jta.append ( " " );
jta.append( myToHexString(mmf.get (),2) );
}
jta.append ( "\n" );
}
} catch (IOException ex)
{
jta.setText ("Error reading file.");
return;
}
}
}

static String myToHexString ( long n, int c )
{
String h= Long.toHexString ( n );
while( h.length () < c)
{
h = "0" + h;
}
return h;
}

static String myToHexString( byte n, int c )
{
int temp = n;
String h = Integer.toHexString ( 0xFF & temp);
while(h.length ()<c)
h = "0"+h;
return h;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unescaping hex encoded characters in string? sprite Ruby 2 09-02-2010 08:52 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments