aspose file tools*
The moose likes Java in General and the fly likes how to cast array of bytes into array of int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to cast array of bytes into array of int" Watch "how to cast array of bytes into array of int" New topic
Author

how to cast array of bytes into array of int

Joel Truc
Greenhorn

Joined: Jun 19, 2002
Posts: 7
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 ?
Irene Loos
Ranch Hand

Joined: Apr 15, 2002
Posts: 78
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 ]

Irene Loos
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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...
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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

Joined: Jun 19, 2002
Posts: 7
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

Joined: Jul 27, 2001
Posts: 1365
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

Joined: Jun 27, 2000
Posts: 286
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to cast array of bytes into array of int