Hi all,

I am stucked at this problem - net to let user set string as byte eg. "0xFF", And parse it to byte. Problem is, that using...

byte byteVar = (byte) "0xFF";

...is incompatible. Nice page about this is there.

Any idea how solve this? Thx for your help..

In Jave SE the Byte class has a constructor that takes a String as parameter:

byte byteVar = new Byte("0xFF");

I don't know if ME will do that unboxing, so maybe

byte byteVar = (new Byte("0xFF")).byteValue() ;

You can also use

Byte.parseByte("0xFF")

method to parse String into Byte , I generally use this way to convert string into any other data type

e.g. from String to Integer

Integer.parseInt("123")

Thx for your reply. Unfortunately I tried using "parse" but ...wrote little "B" oops :]... And prefix "0x" can not be used. So this is working without any error:

byte testByte = Byte.parseByte("c3");

But despite that my implemetation was not working. After a while of testing I find out that problem must somewhere in parsing. This works...

byte testByte1 = (byte)0xc3;

But not this:

byte testByte2 = Byte.parseByte("c3");

... i don't understand, maybe there is problem with hexadecimal number. So i try:

byte testByte2 = Byte.parseByte("c3", 16);

...but still nothing :(

EDIT:

Clue:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5015891[/B]

Hi Ross,

byte testByte2 = Byte.parseByte("c3");

This will not work it will give you
Exception in thread "main" java.lang.NumberFormatException: For input string: "c3"
because this version of parseByte() accept value in decimal so only digits are applicable.

byte testByte2 = Byte.parseByte("c3", 16);

This is correct way of using this method if you are parsing Hexa decimal number but again "c3" is out of byte's range its decimal value is "195" while byte can only accept upto 127.

hope this helps.

Thanks
Javin