aspose file tools*
The moose likes Beginning Java and the fly likes Recvn & Converting Data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recvn & Converting Data?" Watch "Recvn & Converting Data?" New topic
Author

Recvn & Converting Data?

Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I have a class with elements in it that are all int's. I have recvd bytes into a StringBuffer from a InputStream and now want to get that StringBuffer copied into the class? The problem is I don't see anyway to convert or copy a StringBuffer into the class? In C you could use a memcpy to copy an array of bytes into a structure, is there some way of doing that in Java? Should I recv the bytes and store them in a ByteArray and then convert those some how? What do you recommend is the best way to recv the data in order to get it into the individual int elements of the class?
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Can you post your code here?
Are you trying to copy from the StringBuffer to an array of ints - one value at a time or
all valies at once?
If you are receiving bytes from the stream, why not add them to the array right away, before moving them to the StringBuffer?
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
Below is the class and the code to perform the recv of the data. The data coming in is a array of bytes which are all int values to be placed in the class. If I can copy them directly into the class elements that would be fine, how do you do that? The data coming in is being recvd byte by byte so I need to store it somewhere and then convert it into a int, I think?!
public class ConfigurationInformation
{
int nPingRate = 660;
int nReportInterval = 10;
int nVelocity = 70;
int nDwellTime = 30;
};
StringBuffer recvcfg = new StringBuffer();
int ch;
// Open the input stream and read.
is = sc.openInputStream();
while((ch = is.read()) != '\0') {
recvcfg.append((char) ch);
}
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You don't really give enough information to go on. What is the format of the data. From your code it is just dumped into a StringBuffer. Are there separators between elements or are you just stuffing it into the buffer.
Is it like
123456789101112
or
1 2 3 4 5 6 7 8 9 10 11 12
If it is like the first, you can't break that into separate ints. The second look at StringTokenizer.
Bill Ceglia
Ranch Hand

Joined: Dec 13, 2001
Posts: 30
I thought I had made it clear by stating it was an array of bytes and that would mean one byte after another. There are no delimiters or I would have tried to use those. So, I guess the question remains how to take an array of bytes and place it into a class, similar to taking an array of bytes and doing a memcpy into a structure in C?!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recvn & Converting Data?