From: Dirk Bruere at NeoPax on 27 Mar 2010 19:56 How do I actually write hex bytes into a byte array? The obvious method gives me an error ie myArray[0] = 0xc9; -- Dirk http://www.transcendence.me.uk/ - Transcendence UK http://www.blogtalkradio.com/onetribe - Occult Talk Show
From: Peter Duniho on 27 Mar 2010 20:31 Dirk Bruere at NeoPax wrote: > 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
From: Dirk Bruere at NeoPax on 27 Mar 2010 20:43 On 28/03/2010 00:31, Peter Duniho wrote: > Dirk Bruere at NeoPax wrote: >> 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. -- Dirk http://www.transcendence.me.uk/ - Transcendence UK http://www.blogtalkradio.com/onetribe - Occult Talk Show
From: Mike Schilling on 27 Mar 2010 22:35 Peter Duniho wrote: > Dirk Bruere at NeoPax wrote: >> 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.
From: Mike Amling on 27 Mar 2010 23:06 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. --Mike Amling
|
Next
|
Last
Pages: 1 2 Prev: Writing jnlp program for both sandbox and all-permissions Next: BufferedReader vs NIO Buffer |