![]() |
|
|
|
#1 |
|
Posts: n/a
|
When trying to initializate the byte array msg like: byte[] msg = {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d , 0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57, 0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf, 0x8e,0x51,0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11} ; I get the following error: TCPComm/App/Main.java [27:1] possible loss of precision found : int required: byte How to cast one type to another? and Is there a better way to do this initialization? Thank you again! Roberto. |
|
|
|
#2 |
|
Posts: n/a
|
"Roberto Gallo" <> wrote in message
news:bv8b9h$n5a$... > > > When trying to initializate the byte array msg like: > > byte[] msg = {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d , > 0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57, > 0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf, > 0x8e,0x51,0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11} ; > > I get the following error: > > TCPComm/App/Main.java [27:1] possible loss of precision > found : int > required: byte > > How to cast one type to another? and Is there a better way to do > this initialization? > > Thank you again! > > Roberto. Casting is easy: byte b = (byte)someNumber; But you have a different problem. 0xC1 and 0xBE (to name a couple) are both out of range for a byte (-128 to 127). That's why you're getting the error. |
|
|
|
#3 |
|
Posts: n/a
|
Roberto Gallo wrote:
> Is there a better way to do this initialization? char[] _msg = ( "\153\301\276\342\056\100\237\226\351\075" + "\176\021\163\223\027\052\256\055\212\127" + "\036\003\254\234\236\267\157\254\105\257" + "\216\121\060\310\034\106\243\134\344\021" ).toCharArray(); byte[] msg = new byte[_msg.length]; for(int i=0;i<msg.length;++i) msg[i] = (byte)(_msg[i]&0xff); |
|
|
|
#4 |
|
Posts: n/a
|
Thomas Schodt <"news04jan"@\"xenoc.demon.co.uk\"> scribbled the following:
> Roberto Gallo wrote: >> Is there a better way to do this initialization? > char[] _msg = ( > "\153\301\276\342\056\100\237\226\351\075" + > "\176\021\163\223\027\052\256\055\212\127" + > "\036\003\254\234\236\267\157\254\105\257" + > "\216\121\060\310\034\106\243\134\344\021" > ).toCharArray(); > byte[] msg = new byte[_msg.length]; > for(int i=0;i<msg.length;++i) msg[i] = (byte)(_msg[i]&0xff); As previously said, byte is signed in Java, so you'll end up with some negative values in there. You have to take those into account if you're going to use the bytes in arithmetic calculations. -- /-- Joona Palaste () ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "No, Maggie, not Aztec, Olmec! Ol-mec!" - Lisa Simpson |
|
|
|
#5 |
|
Posts: n/a
|
Very nice.... But how did you transform from hex to octal in your reply? Thank tou. "Thomas Schodt" <"news04jan"@\"xenoc.demon.co.uk\"> wrote in message news:bv8evh$d56$1$... > Roberto Gallo wrote: > > > Is there a better way to do this initialization? > > char[] _msg = ( > "\153\301\276\342\056\100\237\226\351\075" + > "\176\021\163\223\027\052\256\055\212\127" + > "\036\003\254\234\236\267\157\254\105\257" + > "\216\121\060\310\034\106\243\134\344\021" > ).toCharArray(); > byte[] msg = new byte[_msg.length]; > for(int i=0;i<msg.length;++i) msg[i] = (byte)(_msg[i]&0xff); |
|
|
|
#6 |
|
Posts: n/a
|
Roberto Gallo <> scribbled the following:
> Very nice.... > But how did you transform from hex to octal in your reply? Maybe he calculated it in his head? -- /-- Joona Palaste () ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "How can we possibly use sex to get what we want? Sex IS what we want." - Dr. Frasier Crane |
|
|
|
#7 |
|
Posts: n/a
|
Joona I Palaste wrote:
> Roberto Gallo <> scribbled the following: > >> Very nice.... > > >> But how did you transform from hex to octal in your reply? > > > Maybe he calculated it in his head? > Yep, not a problem for us old hands, it was only a few not very large numbers (up to 0xff) that needed converting from hex to oct. If you have a lot of the stuff and need some automation you can modify the perl script I posted here http://makeashorterlink.com/?C11442937 |
|
|
|
#8 |
|
Posts: n/a
|
Roberto Gallo wrote:
> When trying to initializate the byte array msg like: > > byte[] msg = {0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d , > 0x7e,0x11,0x73,0x93,0x17,0x2a,0xae,0x2d,0x8a,0x57, > 0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf, > 0x8e,0x51,0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11} ; > > How to cast one type to another? and Is there a better way to do > this initialization? For any values starting with '8', '9', 'a', 'b', 'c', 'd', 'e', and 'f', add a cast to byte. byte[] msg = {0x6b,(byte)0xc1,(byte)0xbe,(byte)0xe2,0x2e,0x40,( byte)0x9f,(byte)0x96,(byte)0xe9,0x3d, .... However, for large items that generates inefficient bytecodes. > and Is there a better way to do > this initialization? > Probably include it as a file in your .jar and use Class.getResourceAsStream() to read it in. |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| .net framework initialization erro | Horvath3081 | General Help Related Topics | 1 | 06-22-2009 12:51 AM |
| constants as of array of integers, for loops | octavsly | Hardware | 0 | 04-25-2009 10:53 AM |
| How to retrieve array parameter ( JAVA ) | naruponk | Software | 1 | 04-16-2009 09:20 AM |
| Array Programme | rits | Software | 2 | 03-04-2009 04:18 PM |
| Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/ | emerald | Software | 3 | 05-14-2006 03:47 AM |