Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Java - array initialization.

 
Thread Tools Search this Thread
Old 01-28-2004, 11:52 AM   #1
Roberto Gallo
 
Posts: n/a
Default array initialization.



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.




  Reply With Quote
Old 01-28-2004, 12:04 PM   #2
Ryan Stewart
 
Posts: n/a
Default Re: array initialization.

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


  Reply With Quote
Old 01-28-2004, 12:54 PM   #3
Thomas Schodt
 
Posts: n/a
Default Re: array initialization.

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);
  Reply With Quote
Old 01-28-2004, 01:23 PM   #4
Joona I Palaste
 
Posts: n/a
Default Re: array initialization.

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
  Reply With Quote
Old 01-28-2004, 04:28 PM   #5
Roberto Gallo
 
Posts: n/a
Default Re: array initialization.


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



  Reply With Quote
Old 01-28-2004, 05:17 PM   #6
Joona I Palaste
 
Posts: n/a
Default Re: array initialization.

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
  Reply With Quote
Old 01-28-2004, 06:42 PM   #7
Thomas Schodt
 
Posts: n/a
Default Re: array initialization.

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
  Reply With Quote
Old 02-07-2004, 11:55 PM   #8
Jon A. Cruz
 
Posts: n/a
Default Re: array initialization.

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.

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47