Hi All,
I want to covert the byte to hex encoded string.
Any pointers how to do that , would be really appreciated.
Regards,
Gaurav
Thomas Kellerer - 25 Sep 2006 12:23 GMT
gauravs_mailbox@yahoo.com 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
gauravs_mailbox@yahoo.com - 25 Sep 2006 13:46 GMT
> gauravs_mailbox@yahoo.com wrote on 25.09.2006 13:20:
> > Hi All,
[quoted text clipped - 6 lines]
>
> 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 ?
Thomas Kellerer - 25 Sep 2006 13:57 GMT
gauravs_mailbox@yahoo.com wrote on 25.09.2006 14:46:
>> gauravs_mailbox@yahoo.com wrote on 25.09.2006 13:20:
>>> Hi All,
[quoted text clipped - 12 lines]
> 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?
Thomas Schodt - 25 Sep 2006 15:57 GMT
> 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>
Red Orchid - 25 Sep 2006 16:02 GMT
gauravs_mailbox@yahoo.com wrote or quoted in
Message-ID: <1159183251.406057.71460@e3g2000cwe.googlegroups.com>:
> 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>
Mark Space - 26 Sep 2006 23:36 GMT
> Hi All,
>
[quoted text clipped - 4 lines]
> 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;
}