• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to cast array of bytes into array of int

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C++ you can do:

in Java i need to do same thing... How can i do plz ?

In fact i have a String that contain my data, i want to read this data int by int. I found the String.getBytes() method but i need int
Any solutions without making a loop and convert values one by one ?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just cast it like this:

In regards of your string data you can use the following but be aware that when you cast char to integer it will be integer representation of char (so char 1 will be repesented in int as 49).

[ June 20, 2002: Message edited by: Irene Loos ]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chars and ints both use the same amount of storage space and String comes with a handy toCharArray(), but unfortunately char[] cannot be casted to int[] and System.arraycopy() won't take different types as arguments. In other words: there is no one line conversion. Though I'm not much of a C++ programmer, I don't think that C++ code would work either (due to memory issues and lack of a C++ type byte ).
If it is possible to simply use a char[], that would be nice. If you must pass an int[] to some other function or in some other way interface with other code, you'll just have to copy each element individually.
It might be useful though if java had some convenient class with a castToInt(char[]) function...
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a lot is going to depend on your string data. what format is it in? if it has more than one peice of data in it, you are going to have to parse it anyways. What are you trying to do with the data? I don't think you are after the results that getBytes will return. those would just be the byte values of the characters that make up the number string(like Irene said), not the numbers you want them to represent.
I think you are going to have to put it in a loop, but you should be able to go directly to int values.
try something like this inside a short for loop:
 
Joel Truc
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thx for your answers.
I ll try to explain better.
My String is a raw data image.
it must look like :"&��_�vr��_7*$�="
each char represente One byte
you know that you need 4 char to make an int
What i want to do is:

But I hope i can avoid the for(.... and have a one line command...

Keep in mind that i dont want to convert each byte in int but 4 byte to one int
Thx everyone.
PS: In C++ it s possible it s a really simple cast.
[ June 21, 2002: Message edited by: Jim Yingst ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this short C++:

Compiled by bcc32, it outputs this on my computer:

Did I make some mistake other than casting?
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid you're stuck with the loop. As far as I know, java doesn't allow you to manipulate the viewing of bits stored in an array. C++ evidently lets you choose to look at the bits of different elements of an array all scrunched together(thus you can take 4 byte elements and view/cast them all together to define an int).
What's so bad about your loop anyway? byte shift operations should be very fast. And anytime I can get a loop down to a single line, I'm happy.
if you needed to take it to an extreme because you will be doing millions of these loops, I supose you could write a native method in C++ and specificly cast the byte[] to an int[] and then handback the int[]. I wouldn't go through the effort and pain of JNI tho unless you were really going to be using that function massive amounts of times AND your program is realtime, or very time sensitive.
Sorry I couldn't give you the answer you wanted. Hope something in here helps.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did I make some mistake other than casting?
You're reading 20 bytes from an array that only has 5 bytes initialized in it. The first time through the loop, ints[0] grabs the first 4 bytes and converts them into an int. Apparently your system is little endian:
67305985 = (((4 * 256 + 3) * 256 + 2) * 256 + 1
Next time through the loop, ints[1] grabs the next 4 bytes - 3 of which are outside your original 5-byte loop. Naturally things get worse after that.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joel- I'm a bit suspicious of where this String came from. If you read it in from a file or data stream somewhere, it's probably going to be easier to never even convert it to a String in the first place. Try using a DataInputStream instead. The readInt() method will grab 4 bytes and create an int out of them (big endian). If you convert a String to byte[] or vice versa, you're using the platform default encoding. This is risky if you're passing data from one machine to another, which may have a different default encoding. You can get around this by specifying an encoding each time you use getBytes() or new String(byte[]), but this is tedious. Plus, some byte sequences may be illegal in certain encodings, even though they represent valid images. If the data doesn't represent text, you probably shouldn't depend on the String class to process it. DataInputStream and DataOutputStream are designed to handle arbitrary binary data.
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic