byte array

  • Thread starter Dirk Bruere at NeoPax
  • Start date
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;
 
D

Dirk Bruere at NeoPax

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

Thanks.
I saw the error of my ways just before this came up - I did a cast.
I'm going to bed.
 
M

Mike Schilling

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;"

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.
 
M

Mike Amling

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.

--Mike Amling
 
A

Arne Vajhøj

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.

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
 
L

Lew

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 ;-)

Neither of the literals in Pete's example is an eight-bit literal.
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.

And the other 1% should have done so. Code isn't just about making
the computer do the right thing, it's about documenting that thing for
future maintenance programmers.
 
D

Daniel Pitts

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?
The only unsigned primitive numeric data type in Java is char.
 
A

Arne Vajhøj

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.
you mean signed, right?

Yes.

:-(

Arne
 
K

Kevin McMurtrie

Arne Vajhøj said:
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.
you mean signed, right?

Yes.

:-(

Arne

As somebody who occasionally writes data processing code, I'll join you
in some cursing.

I also curse not being able to allocate arrays of object instances.
Sun's GC algorithms barely keep pace with high-end use. You'd think
this would be a priority feature to help application servers and number
crunchers.
 

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,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top