D
Dirk Bruere at NeoPax
How do I actually write hex bytes into a byte array?
The obvious method gives me an error ie
myArray[0] = 0xc9;
The obvious method gives me an error ie
myArray[0] = 0xc9;
Dirk said:How do I actually write hex bytes into a byte array?
The obvious method gives me an error ie
myArray[0] = 0xc9;
In general, if you are asking a question related to an error you are
seeing, you should post specific information about the error.
And if you are asking a question related to some specific code you are
trying to get to work, you should post a complete enough example of the
code so that at least there is no question about how variables and types
are declared, if not a fully compilable example (see http://sscce.org/)
That said, it may help you to know that "byte" in Java is a signed type,
supporting values only from -128 to 127 (0x80 to 0x7f). The value 0xc9
is the same as 201 decimal, which is outside that range.
The code you posted, assuming "myArray" is declared as:
byte[] myArray;
is essentially the same as this:
int i = 0xc9;
byte b = i;
And hopefully you can see why the above doesn't work.
You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] = 0xffffffc9;"
Pete
Peter said:Dirk said:How do I actually write hex bytes into a byte array?
The obvious method gives me an error ie
myArray[0] = 0xc9;
In general, if you are asking a question related to an error you are
seeing, you should post specific information about the error.
And if you are asking a question related to some specific code you are
trying to get to work, you should post a complete enough example of
the code so that at least there is no question about how variables
and types are declared, if not a fully compilable example (see
http://sscce.org/)
That said, it may help you to know that "byte" in Java is a signed
type, supporting values only from -128 to 127 (0x80 to 0x7f). The
value 0xc9 is the same as 201 decimal, which is outside that range.
The code you posted, assuming "myArray" is declared as:
byte[] myArray;
is essentially the same as this:
int i = 0xc9;
byte b = i;
And hopefully you can see why the above doesn't work.
You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
Mike said:Peter said:You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
How intuitive that an 8-bit literal doesn't fit into an 8-bit byte, but a
32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast rather
than try to type the right number of 'f's.
Mike said:Peter said:You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
How intuitive that an 8-bit literal doesn't fit into an 8-bit byte,
but a 32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast
rather than try to type the right number of 'f's.
To avoid the counting of Fs, you can also use
myByteArray[0]=0xC9-256;
to assign 0x80..0xFF to a byte.
Peter said:You can fix the code in at least one of two ways:• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
Mike said:How intuitive that an 8-bit literal doesn't fit into an 8-bit byte, but a
32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast rather
than try to type the right number of 'f's.
you mean signed, right?Mike said:Peter Duniho wrote:
You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
How intuitive that an 8-bit literal doesn't fit into an 8-bit byte,
but a 32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast
rather than try to type the right number of 'f's.
To avoid the counting of Fs, you can also use
myByteArray[0]=0xC9-256;
to assign 0x80..0xFF to a byte.
My suggestion for easiest and most readable code is to cast
all byte values from 0x00 to 0xFF.
And occasionally spend some minutes cursing whoever
decided that byte was unsigned.
you mean signed, right?Mike Schilling wrote:
Peter Duniho wrote:
You can fix the code in at least one of two ways:
• cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
• specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
How intuitive that an 8-bit literal doesn't fit into an 8-bit byte,
but a 32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast
rather than try to type the right number of 'f's.
To avoid the counting of Fs, you can also use
myByteArray[0]=0xC9-256;
to assign 0x80..0xFF to a byte.
My suggestion for easiest and most readable code is to cast
all byte values from 0x00 to 0xFF.
And occasionally spend some minutes cursing whoever
decided that byte was unsigned.
Arne Vajhøj said:you mean signed, right?On 27-03-2010 23:06, Mike Amling wrote:
Mike Schilling wrote:
Peter Duniho wrote:
You can fix the code in at least one of two ways:
€ cast the literal to byte, forcing the conversion: "myArray[0] =
(byte)0xc9;"
€ specify a literal that can fit in a byte: "myArray[0] =
0xffffffc9;"
How intuitive that an 8-bit literal doesn't fit into an 8-bit byte,
but a 32-bit literal does ;-)
A bit more seriously, I presume that 99% of people would use the cast
rather than try to type the right number of 'f's.
To avoid the counting of Fs, you can also use
myByteArray[0]=0xC9-256;
to assign 0x80..0xFF to a byte.
My suggestion for easiest and most readable code is to cast
all byte values from 0x00 to 0xFF.
And occasionally spend some minutes cursing whoever
decided that byte was unsigned.
Yes.
:-(
Arne
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.