Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Decoding a hex string to byte [] from an encoded byte array

807606Feb 27 2007 — edited Feb 28 2007
Hi guy,

I need to store an image as a String, this would not be as hard if the String didnt need to be in "hex" format, i can convert the image to byte array. and i have this code to convert it to a String, but i am not sure how to convert it back to a byte array.. the same byte array.
 /**
     *Takes in a byte array and converts in to a stirng array of hex
     */
    private static String toHexString( byte[] b )
    {
        
        // table to convert a nibble to a hex char.
        char[] hexChar = {
            '0' , '1' , '2' , '3' ,
            '4' , '5' , '6' , '7' ,
            '8' , '9' , 'a' , 'b' ,
            'c' , 'd' , 'e' , 'f'};
        
        StringBuffer sb = new StringBuffer( b.length * 2 );
        for ( int i=0; i<b.length; i++ )
        {
            // look up high nibble char
            sb.append( hexChar [( b[i] & 0xf0 ) >>> 4] );
            
            // look up low nibble char
            sb.append( hexChar [b[i] & 0x0f] );
        }
        
        return sb.toString();
        
    }
Thanks please can you explain how to reconvert it back to the byte array, or if that is not going to work some help with writting a way that this can be done.

Many thanks
David
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 28 2007
Added on Feb 27 2007
11 comments
941 views