Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recvn & Converting Data?

 
Bill Ceglia
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic