byte array initialization with Hex values

In have a problem with this:byte[] cmd = {0x00, 0xFF, 0x04, 0x04};The compiler complaints om 0xFF saying:Type mismath: cannot convert from int to byte what do I do wrong?/Helena
[219 byte] By [HelenaStarMountaina] at [2007-11-26 17:54:37]
# 1
Because byte can take values in the range -128 to +127.So the eighth bit can't be 1 as it would exceed +127 and even casting it to a byte would then give a negative value.
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 2
A byte is in the range -128 to +127, and 0xff is 255, so it's out of range.Kaj
kajbja at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 3
The maximum value you can give is 0x7FH.
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 4

Of course you can explicitly cast the >127 values to keep the compiler's

mouth shut:byte[] array= { (byte)0xff, (byte)0x80, 0x7f, 0x00 };

kind regards,

Jos

JosAHa at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 5
(byte)0xff is the same as the byte -127Kaj
kajbja at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 6
Don't forget the dukes.
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 7
> (byte)0xff is the same as the byte -127Wrong, it's -1-127 would be 0x81 in hexadecimal notation.kind regards,Jos
JosAHa at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 8
> > (byte)0xff is the same as the byte -127> > Wrong, it's -1Sigh, I just realised that after I had clicked post :(
kajbja at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 9
> > > (byte)0xff is the same as the byte -127> > > > Wrong, it's -1> > Sigh, I just realised that after I had clicked post :(:-)kind regards,Jos - Kaj : 1 - 0 :-P
JosAHa at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 10
> > (byte)0xff is the same as the byte -127> > Wrong, it's -1> > -127 would be 0x81 in hexadecimal notation.Precisely.A mere two's complement.
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 11
> Jos - Kaj : 1 - 0 :-PDon't you mean11111111 - 10000001 ;)
kajbja at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 12
> Jos - Kaj : 1 - 0 :-PAh.Ah.That looks like a game of football.
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 13
> > Jos - Kaj : 1 - 0 :-P> > Don't you mean> > 11111111 - 10000001 ;)That would still make it -1 and -127.So mathematically Jos's score is bigger :D
qUesT_foR_knOwLeDgea at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 14

> > > Jos - Kaj : 1 - 0 :-P

> >

> > Don't you mean

> >

> > 11111111 - 10000001 ;)

>

> That would still make it

>

> -1 and -127.So mathematically Jos's score is bigger :D

That was the point :)

kajbja at 2007-7-9 5:07:39 > top of Java-index,Java Essentials,New To Java...
# 15

OK guys, thanks for the zeros-ones-game. Good dribbling.

My colluege got back from lunch and told me that byte alwas get "converted" to ints. Hmm, lots to learn in the basics.

Thanks for you help, I will spread my dukes among you :-)

I casted it to (byte) and the problem will be solved...

/Helena

HelenaStarMountaina at 2007-7-9 5:07:40 > top of Java-index,Java Essentials,New To Java...
# 16

> My colluege got back from lunch and told me that byte alwas get

> "converted" to ints. Hmm, lots to learn in the basics.

It's a bit more subtle than that. Given a binary expression "L op R" where

L and R are both operands to the operator "op". If both L and R are

narrower than (or equal to) an int, both operand types will be cast to ints.

This casting is performed by using sign extension, i.e. the sign bit of

the originial type will be used for all the bits in the wider type.

If one of the types is wider than an int, e.g. a long, strange things can

happen if you don't pay attention. Have a look at this example:int x= 0x7fffffff;

int y= 0x7fffffff;

long z= 2;

System.out.println(x+y+z);

System.out.println(x+(y+z));

> Thanks for you help, I will spread my dukes among you :-)

Thanks for the duke thingies.

kind regards,

Jos

JosAHa at 2007-7-9 5:07:40 > top of Java-index,Java Essentials,New To Java...