Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Someone had this thought and I started thinking about it.
To send an integer as sequence of ASCII symbols over network.

For example, say I have integer 123.

Then over network, I send ASCII code for "1" "2" and "3".

C++
byte sendData[255];
sendData[0] = 49; // ASCII code for "1"
sendData[1] = 50; // ASCII for "2" etc,
sendData[2] = 51;
send(sendData,3); // 3-size

Of course the receiver will be able to parse it and obtain "123".

What are the pros and cons of this method as compared to sending an integer?
e.g., instead of sending ASCII codes of integer to send its binary representation
as it is usually done.

e.g.,
C++
int x = 123; 
send(x,4);
// One problem with this approach is endianness; I think above method does not suffer from endianness problems, am I right?
Posted
Updated 26-Oct-13 6:31am
v6

1 solution

Sending it as a string has almost only disadvantages. You may want to send it in this format probably only in the following scenarios:
- The receiver application/device is not yours and it expects you to send the integer in string format.
- You send it in string format because you are able to sniff/record the network traffic and text format makes debugging easier because the data traffic is human readable. In same cases I'm using a define to be able to switch between human readable and binary serialization and I use human readable only while debugging.

Another advantage of the text format is that it automatically solves byte ordering/endianness issues between different architectures but handling this in code with binary transfer is also an easy thing.
 
Share this answer
 
Comments
_coder 26-Oct-13 13:56pm    
I agree with you, also as I mentioned, I think string approach solves endianness issues. But on the other hand handling endianness issues with binary serialization is not that hard too. Was wondering if string approach had some other issues too?
pasztorpisti 26-Oct-13 14:08pm    
There is a lot of cpu and memory overhead when converting data to/from string and it usually multiplies the network traffic too but these are the only issues (that can be serious in some cases). It is a lot of waste but in case of a simply home/intranet application that is not cpu intensive and doesn't make a lot of net traffic you can go with strings.
_coder 26-Oct-13 15:22pm    
Ok, I see, thanks.
pasztorpisti 26-Oct-13 15:29pm    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900