• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion ?

 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Integer.toHexString(int i)
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it suppresses 0 . How can I get output of 0x01 as 0001 and want a byte array rather than String.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic