Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

How: convert byte[] to hex encoded string

Thread view: 
gauravs_mailbox@yahoo.com - 25 Sep 2006 12:20 GMT
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;
    }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.