整数为网络上的ascii [英] integer as ascii over network

查看:83
本文介绍了整数为网络上的ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有这个想法,我开始考虑它。

通过网络发送一个整数作为ASCII符号序列。



例如,假设我有整数123.



然后通过网络,我发送12和3的ASCII码。



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".

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



当然接收器将能够解析它并获得123。



与发送整数相比,此方法的优缺点是什么?

例如,而不是发送整数的ASCII码来发送它的二进制表示

,因为它通常是完成的。



例如,


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.,

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?

推荐答案

将其作为字符串发送几乎有唯一的缺点。您可能希望以这种格式发送它可能仅在以下情况下:

- 接收器应用程序/设备不是您的,它希望您以字符串格式发送整数。

- 您以字符串格式发送它,因为您能够嗅探/记录网络流量,文本格式使调试更容易,因为数据流量是人类可读的。在相同的情况下,我使用一个define来能够在人类可读和二进制序列化之间切换,我只在调试时使用人类可读。



文本的另一个优点格式是它自动解决不同体系结构之间的字节排序/字节顺序问题,但在代码中使用二进制传输处理它也是一件容易的事。
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.


这篇关于整数为网络上的ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆