jtextfield string to byte
hi all,
i am trying to do something basic, but i cant make it...
i am trying to enter strings in a jtextfield (actually hexadecimal commands). theses hex commands needs to be sent to a class in a byte array format. (or short array format)
i have tried everything to convert the string to a byte
(valueOf, parseByte, decode, several step by step cast, (short)Integer.parseInt(b1.trim()), byte [] b = s.getBytes( "8859_1" /* encoding */ ); etc),
but i cant make it. am i missing something basic with the string coming from the jtextfield? am i missing something basic in the conversion?
thanks for your help!
[651 byte] By [
fl88uja] at [2007-11-27 8:08:59]

# 1
I think your problem is your trying to take a string representation of a byte and turn that into a byte, as opposed to converting the string to bytes. That might not be a helpful explanation, but here is what I mean.
The string "A" converted to a byte is 65.
The string "65" converted to bytes is 54 and 53, but the bytes 54 and 53 converted back to a String is not "A", it's "65".
So putting the byte values into a text field and then converting it to a String and then asking for the bytes of that String won't get you what you're looking for.
I think you'll need to parse the String into it's hexidecimal components and then work on converting those individual components to bytes.
# 2
thanks for your tip.
i think i am approching what i need. i have implemented this:
try{
hex = Integer.parseInt(b1.trim(), 16 /* radix */);
mybyte = (byte)hex;
} catch (Exception ex)
{
System.out.println(ex);
}
and it seems ok.
but wont i then have exactly the same problem with the hexadecimal conversion? and another question, how can i decipher my hex value in binary? that is, if i enter the string FF, i would have FF as an hex value, and 1111111 as a bin?
thanks again!