TODO:字节序的一些理解

TODO:字节序的一些理解

本文是小编对字节序的片面理解,希望对你有帮助哈。

字节序,即字节在电脑中存放时的序列与输入(输出)时的序列是先到的在前还是后到的在前。

1.Little endian:将低序字节存储在起始地址,俗称小端

2.Big endian:将高序字节存储在起始地址,俗称大端

比如一个0x01020304(左高右低)这个整数,在小端下在内存中的布局如下

[04][03][02][01] 注意左边是低地址,而右边是高地址

在大端下则是

[01][02][03][04] 注意左边是高地址,而右边是低地址。

比较大端,小端的排序,采用大端方式存储数据是比较符合人类的思维习惯。

因为网上有些人对大端,小端的认识不一样;等下小编会写一段golang的代码来论证小端,大端排序的正确性。

网络字节序,是指网络传输的字节序,可能是大端序或者小端序,取决于软件开始时通讯双方的协议规定。TCP/IP协议RFC1700规定使用“大端”字节序为网络字节序,开发的时候需要遵守这一规则。

在现有的平台上Intel的X86采用的是Little-Endian,而像Sun的SPARC采用的就是Big-Endian。在C语言中,默认是小端(在有些单片机的实现中是基于大端,比如Keil 51C),Java是平台无关的,默认是大端,golang也是默认使用大端。不同语言各自采用不同的字节序,相互之间的传递需要进行字节序的转换工作。

最后,附上Golang对大小端的测试代码

输出:

a: 305419896

BigEndian: 12 34 56 78

b: 305419896

c: 305419896

tt: 0x12345678

//tt输出0x123456,则表示Go使用大端。如果需要对小端测试只要把BigEndian改成LittleEndian即可,有兴趣可以试试。


wxgzh:ludong86

qrcode_for_gh_6bb1f39ae99c_258-1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值