How: convert byte[] to hex encoded string

Discussion in 'Java' started by gauravs_mailbox@yahoo.com, Sep 25, 2006.

  1. Guest

    Hi All,

    I want to covert the byte to hex encoded string.

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

    Regards,
    Gaurav
     
    , Sep 25, 2006
    #1
    1. Advertising

  2. 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
     
    Thomas Kellerer, Sep 25, 2006
    #2
    1. Advertising

  3. Guest

    Thomas Kellerer wrote:
    > 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 ?
     
    , Sep 25, 2006
    #3
  4. wrote on 25.09.2006 14:46:
    > Thomas Kellerer wrote:
    >> 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?
     
    Thomas Kellerer, Sep 25, 2006
    #4
  5. 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>
     
    Thomas Schodt, Sep 25, 2006
    #5
  6. Red Orchid Guest

    wrote or quoted in
    Message-ID: <>:

    >
    > 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>
     
    Red Orchid, Sep 25, 2006
    #6
  7. Mark Space Guest

    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,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;
    }
     
    Mark Space, Sep 26, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,405
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    534
    Juha Autero
    Aug 19, 2003
  3. jt
    Replies:
    3
    Views:
    964
    Keith Thompson
    May 23, 2005
  4. chirs
    Replies:
    3
    Views:
    192
    Dr John Stockton
    Dec 1, 2003
  5. Laura Schmidt
    Replies:
    21
    Views:
    768
    Andreas Leitgeb
    Nov 24, 2013
Loading...

Share This Page