how to convert a byte array with string,int and double embeded?

C

chrisben

Hi,

I send out a byte array through socket and then try to convert the data
back. However, I run into some problems when I mix string with other datatypes

For example

sender array
byte[] data = new byte[32];

string name = "chris"
string nickName = "CBen"

MemoryStream stream = new MemoryStream(data, 0, 115);
BinaryWriter writer = new BinaryWriter(stream);

byte[] nm = System.Text.Encoding.UTF8.GetBytes(name);
byte[] nickName = System.Text.Encoding.UTF8.GetBytes(nickName);

writer.Write(bStk);
writer.Write(bTicker);
writer.Write(24);
writer.Write(23.1)


receiving side (this part does not work)

byte[] data

MemoryStream stream = new MemoryStream(data, 0, 32);
BinaryReader reader = new BinaryReader(stream);
byte[] a = reader.ReadBytes(8);
string name = System.Text.Encoding.UTF8.GetString(a);
intNum = reader.ReadInt32();
string ticker = System.Text.Encoding.UTF8.GetString(reader.ReadBytes(8));


Thanks for your time

Chris

PS. string size must be less than 8, but may vary.
 
J

Jeff Johnson

receiving side (this part does not work)

byte[] data

MemoryStream stream = new MemoryStream(data, 0, 32);
BinaryReader reader = new BinaryReader(stream);
byte[] a = reader.ReadBytes(8);
string name = System.Text.Encoding.UTF8.GetString(a);
intNum = reader.ReadInt32();
string ticker =
System.Text.Encoding.UTF8.GetString(reader.ReadBytes(8));


Thanks for your time

Chris

PS. string size must be less than 8, but may vary.

Well, there's your problem! You aren't telling the other side how many bytes
there are in the string. Without that it blindly reads 8 when in your
example you first write 5 bytes (chris) and then 4 (CBen). (Not only that,
but you write two strings and then only attempt to read one, but the real
issue is the byte count.) Is that REALLY your receiver code?
 
C

chrisben

well, the question is actually is

if I have two strings with variable size, but less then 8 bytes each, plus
one int and one double or float, and they need to packaged to a byte array on
one side and be retrieved from the end user, who is not aware of the exact
length of the two strings, what is the best way to do it?

Thanks

Jeff Johnson said:
receiving side (this part does not work)

byte[] data

MemoryStream stream = new MemoryStream(data, 0, 32);
BinaryReader reader = new BinaryReader(stream);
byte[] a = reader.ReadBytes(8);
string name = System.Text.Encoding.UTF8.GetString(a);
intNum = reader.ReadInt32();
string ticker =
System.Text.Encoding.UTF8.GetString(reader.ReadBytes(8));


Thanks for your time

Chris

PS. string size must be less than 8, but may vary.

Well, there's your problem! You aren't telling the other side how many bytes
there are in the string. Without that it blindly reads 8 when in your
example you first write 5 bytes (chris) and then 4 (CBen). (Not only that,
but you write two strings and then only attempt to read one, but the real
issue is the byte count.) Is that REALLY your receiver code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top