How: convert byte[] to hex encoded string

G

gauravs_mailbox

Hi All,

I want to covert the byte to hex encoded string.

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

Regards,
Gaurav
 
T

Thomas Kellerer

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
 
G

gauravs_mailbox

Thomas said:
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 ?
 
T

Thomas Kellerer

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?
 
R

Red Orchid

(e-mail address removed) wrote or quoted in
Message-ID: said:
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 & 0xff;

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

return s.toString();
}

</code>
 
M

Mark Space

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,8) );
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top