Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Contributor
Posts: 12
Registered: ‎01-22-2013
My Device: 9900
My Carrier: GiffGaff
Accepted Solution

Convert HexaDecimal String to byte array.

Hello,

 

I want to convert hexa decimat string to byte array. I would like this to work on OS5.0+ . In OS6 , there is net.rim.device.api.util.ByteArrayUtilities which has the methods to do this but I need to support OS5 onwards.

 

I have also seen a Hex encoder and decoder at http://www.java2s.com/Code/Java/Data-Type/Hexencoderanddecoder.htm but it uses apache license and I dont want to use that. 

 

Is there any sample code/ documentation some one can provide which would help me to convert a hexa decimal string to a byte array?

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Convert HexaDecimal String to byte array.

you can use http://www.blackberry.com/developers/docs/7.1.0api/java/io/ByteArrayOutputStream.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 12
Registered: ‎01-22-2013
My Device: 9900
My Carrier: GiffGaff

Re: Convert HexaDecimal String to byte array.

this would get me the bytes yes, but i would like to decode a hex encoded data to a byte array. Any thing which will help me to decode?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Convert HexaDecimal String to byte array.

Something like this?

        /**
        * Convert a String in 'hex' to a byte[] array.
        * @param in - String
        * @return result - byte[] buffer
        * 
        * See variation on this in hexStringToByteArray2
        * Not sure which is the best - to be tested
        */
        public static final byte [] hexStringToByteArray(final String in) {
                if ( in == null ) {
                        return null;
                }
                if ( in.length() == 0 ) {
                        return new byte [0];
                }
                if ( ((in.length() >> 1) << 1) != in.length() ) {
                        throw new RuntimeException("Hex String not even number of characters - " + in);
                }
                byte result[] = new byte[in.length()/2];
                int firstNibble, secondNibble, finalbyte;
                int finalByte;
                for (int i=result.length-1; i >= 0; i--) {
                        firstNibble = HEX_UPPER.indexOf(in.charAt(2*i));
                        if ( firstNibble == -1 ) {
                                firstNibble = HEX_LOWER.indexOf(in.charAt(2*i));
                        }
                        secondNibble = HEX_UPPER.indexOf(in.charAt(2*i+1));
                        if ( secondNibble == -1 ) {
                                secondNibble = HEX_LOWER.indexOf(in.charAt(2*i+1));
                        }
                        if ( firstNibble == -1 || secondNibble == -1 ) {
                                throw new RuntimeException("Invalid hex char(s) found, at offset: " + Integer.toString(2*i) +
                                                " - " + in);
                        }
                        finalByte = (secondNibble) | (firstNibble << 4 );
                        result[i] = (byte) finalByte;
                }
                return result;
        }

        /**
        * Convert a String in 'hex' to a byte[] array.
        * @param hex - String
        * @return bytes - byte[] buffer
        * 
        * See variation on this in hexStringToByteArray
        * Not sure which is the best - to be tested
        */
        public static final byte [] hexStringToByteArray2(final String hex) {
                byte [] bytes = new byte[(hex.length() / 2)];
                int j = 0;
                for ( int i=0; i<bytes.length; i++ ) {
                        j = i * 2;
                        String hex_pair = hex.substring(j,j+2);
                        byte b = (byte) (Integer.parseInt(hex_pair, 16) & 0xFF);
                        bytes [i] = b;
                }
                return bytes;
        }

 

Contributor
Posts: 12
Registered: ‎01-22-2013
My Device: 9900
My Carrier: GiffGaff

Re: Convert HexaDecimal String to byte array.

exactly that !!! thanks peter