This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ?
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...
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:
Joined: Jun 19, 2002
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 ]
Joined: Jul 27, 2001
I wrote this short C++:
Compiled by bcc32, it outputs this on my computer:
Did I make some mistake other than casting?
Joined: Jun 27, 2000
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.
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 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 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
Joined: Jan 30, 2000
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.