This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Conversion ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Conversion ?" Watch "Conversion ?" New topic
Author

Conversion ?

A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
How to convert a hex representation ?

Suppose There are some values ,

a= 0x40
b=0x800
c=0x06

How can I convert it into 04080006 representation which should be a byte array ?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Check out Integer.toHexString(int i)


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
But it suppresses 0 . How can I get output of 0x01 as 0001 and want a byte array rather than String.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

A Agr wrote:But it suppresses 0 . How can I get output of 0x01 as 0001 and want a byte array rather than String.


If you want a byte array from an integer, you can either use the java.nio.ByteBuffer class, or fill in the byte array using shifting and AND operations.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
A Agr
Ranch Hand

Joined: Feb 10, 2009
Posts: 32
I tried but didnt work .

My objective is to,

Store hexadecimal nos in either String[] or String or int[].
for ex : String[] s = {"0x09","0x800"}
or int[] i ={0x09,0x800};

Now I want to convert it to byte array to write to an OutputStream. If I do String.getBytes() ,it encodes.
But what I want is my InputStream to read the byte array I send as

byte array containing 090800

How can I do the conversion ? Also in java it is signed byte whose upper limit is 127 so how can I deal with hexadecimal nos greater than 127 ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

A Agr wrote:I tried but didnt work .


What have you tried? And how did it not work? My last post should give you something close, which you can work out.

A Agr wrote:
Also in java it is signed byte whose upper limit is 127 so how can I deal with hexadecimal nos greater than 127 ?


Very carefully...

Seriously, it is possible to treat signed numbers as unsigned numbers, provided that you shift (or cast) it correctly. Hexidecimal literals can be both signed or unsigned -- so if you assign 0xFF into a byte, the compiler will not complain, and knows that you want it unsigned.

It can't print that byte though, as the print mechanism doesn't know that it is unsigned. To do that you need to cast it and AND it, to something bigger before you can print it. Assuming "b" is a byte...

short s = ((short) b) & 0xFF;

Now you can print s, which contains the value of b, if it was unsigned.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

A Agr wrote:Store hexadecimal nos in either String[] or String or int[].
for ex : String[] s = {"0x09","0x800"}
or int[] i ={0x09,0x800};

Now I want to convert it to byte array to write to an OutputStream. If I do String.getBytes() ,it encodes.
But what I want is my InputStream to read the byte array I send as

byte array containing 090800


1. String.getBytes() returns the ASCII values of the string as a byte array. This is not what you want.

2. For the int array, if you use the ByteBuffer class, it will probably do it incorrectly (not sure, haven't used all the features) as it assumes all integers are 4 bytes. So....

int[] i ={0x09,0x800};

should be converted to...

byte[] b = {0x00, 0x00, 0x00, 0x09,0x00, 0x00, 0x08, 0x00};

3. If you do it manually. Create the byte buffer, and shift the int values into place, it should work. Or use the ByteBuffer class, and remove the parts you don't want, that will work too.

Henry
 
jQuery in Action, 2nd edition
 
subject: Conversion ?