Coverting Ascii Hexidecimal Number

J

JVSFugitive

Interesting problem to solve here:

I have an ASCII representation of an integer such as "FF". It is stored
in my Java app as array byte[2] = { 70, 70 };

The question is how would I convert that to it's integer equivalent
which would be in this case equal to 255.

i.e. FF = 1111 1111 binary = 255 decimal
"FF" as ascii representation = { 70, 70 };

Thanks
 
M

Michael Borgwardt

JVSFugitive said:
Interesting problem to solve here:

I have an ASCII representation of an integer such as "FF". It is stored
in my Java app as array byte[2] = { 70, 70 };

The question is how would I convert that to it's integer equivalent
which would be in this case equal to 255.

i.e. FF = 1111 1111 binary = 255 decimal
"FF" as ascii representation = { 70, 70 };

byte[] bytes = new byte[]{70,70};
int integer = Integer.parseInt(new String(bytes,"US-ASCII"), 16);
 
J

JVSFugitive

Michael said:
JVSFugitive said:
Interesting problem to solve here:

I have an ASCII representation of an integer such as "FF". It is
stored in my Java app as array byte[2] = { 70, 70 };

The question is how would I convert that to it's integer equivalent
which would be in this case equal to 255.

i.e. FF = 1111 1111 binary = 255 decimal
"FF" as ascii representation = { 70, 70 };


byte[] bytes = new byte[]{70,70};
int integer = Integer.parseInt(new String(bytes,"US-ASCII"), 16);

Thank you very much :)
 
T

Thomas Weidenfeller

JVSFugitive said:
Interesting problem to solve here:

Interesting? Well ... Please consider posting beginner's questions to
comp.lang.java.help in the future.
I have an ASCII representation of an integer such as "FF". It is stored
in my Java app as array byte[2] = { 70, 70 };

Using bytes for chars in not a good idea in Java. If you can, change
them to chars.
i.e. FF = 1111 1111 binary = 255 decimal
"FF" as ascii representation = { 70, 70 };

Assuming Java 1.5:

byte aBytes[] = new byte[] {70, 70};
int i = Character.digit(aBytes[0] & 0xFF, 16) * 16
+ Character.digit(aBytes[1] & 0xFF, 16);


/Thomas
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top