二进制数据表示 [英] Binary data representation

查看:79
本文介绍了二进制数据表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我目前正在编写序列化/反序列化架构。读/写

函数将从二进制文件读/写。


我的问题是在

表示数据类型(int,int32,int64,double,string等)?

谢谢,


解决方案

Charles T.写道:

我目前正在编写序列化/反序列化架构。读/写
函数将从二进制文件读/写。


为什么是二进制文件?

我的问题是在
表示数据类型时使用某种定义的标准(int,int32, int64,double,string等....)?




编号甚至没有标准什么顺序字节进入int。


最令人发指的数据格式是XML。你可以在其中编写非常简单或非常好的b $ b b复杂数据结构,你可以在文本中读取这些结构

编辑器。


但XML可能有点肥胖。一些数据格式是压缩的XML。


-

Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces


[snip]

最令人发指的数据格式是XML。您可以在其中编写非常简单或非常复杂的数据结构,并且可以在文本编辑器中阅读这些结构。

但XML可能有点肥胖。一些数据格式是压缩的XML。




我也会推荐这个。

游戏Age of Mythology使用与zLib兼容的XML压缩

压缩,它生成非常紧凑但易于解码的文件。

你可以在这里获得zLib:
http://www.zlib.org


- Pete


Charles T.写道:



我目前正在编写序列化/反序列化架构。读/写
函数将从二进制文件读/写。


在这个新闻组和新闻:comp.lang.c中,有很多关于序列化和持久性的讨论。使用搜索引擎,看看

的一些想法。


我的问题是,当
表示数据类型时,有一些定义的标准使用(int,int32,int64,double,string等....)?


从平台到平台没有标准。在某些平台上,

OS版本或编译器版本之间可能没有标准。

为了更好的可移植性,以一致的形式写出数据

(即uint64 == 64位,小端)并让程序将数据转换为原始表示形式。


请记住,序列化时,结构的大小可能不是它的成员大小的总和。允许编译器添加填充字节。会员之间。


指针不能存放好。操作系统将在每个

执行程序的同一位置为变量分配一个变量的概率非常小。


因为指针不能很好地存储,所以不要将字符串存储为指针。

将文本存储为< quantity,text>或者< text,sentinel character> ;.

参见C ++ FAQ(关于序列化)的第[35]节:
http://www.parashift.com/c++-faq-lit...alization.html


谢谢,



-

托马斯马修斯


C ++新闻组欢迎消息:
http://www.slack。 net /~shiva / welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html <其他网站:
http:// www。 josuttis.com - C ++ STL图书馆书


Hi,

I currently writing a serialize/unserialize architecture. The read/write
function will read/write from a binary file.

My question is is there some sort on defined standart to use when
representing data type (int , int32, int64, double, string, etc....) ?
Thanks,


解决方案

Charles T. wrote:

I currently writing a serialize/unserialize architecture. The read/write
function will read/write from a binary file.
Why a binary file?
My question is is there some sort on defined standart to use when
representing data type (int , int32, int64, double, string, etc....) ?



No. There is not even a "standard" for what order bytes go inside an int.

The least heinous data format is XML. You can write very simple or very
complex data structures in it, and you can read those structures in a text
editor.

But XML can be a little obese. Some data formats are compressed XML.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces


[snip]

The least heinous data format is XML. You can write very simple or
very complex data structures in it, and you can read those structures
in a text editor.

But XML can be a little obese. Some data formats are compressed XML.



I would reccomend this, also.
The game Age of Mythology uses XML compressed with zLib compatible
compression, and it generates very compact but easily decoded files.
You can get zLib here:
http://www.zlib.org

- Pete


Charles T. wrote:

Hi,

I currently writing a serialize/unserialize architecture. The read/write
function will read/write from a binary file.
There has been much discussion on Serialization and Persistence in
this newsgroup and news:comp.lang.c. Use a search engine and look
for some ideas.

My question is is there some sort on defined standart to use when
representing data type (int , int32, int64, double, string, etc....) ?
There is no standard, from platform to platform. On some platforms,
there may be no standards between OS versions or compiler versions.
For better portability, write out the data in a consistent form
(i.e. uint64 == 64 bits, little endian) and let the programs convert
the data into the native representation.

Remember, when serializing, that the size of a structure may not
be the sum of the size of its members. Compilers are allowed to
add "padding bytes" between members.

Pointers don''t store well. There is a very small probability
that an OS will allocate a variable in the same place for each
execution of a program.

Since pointers don''t store well, don''t store strings as pointers.
Store text as <quantity, text> or <text, sentinel character>.

See section [35] of the C++ FAQ (about serialization):
http://www.parashift.com/c++-faq-lit...alization.html


Thanks,


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


这篇关于二进制数据表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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