具有“char”的网络字节顺序转换 [英] Network byte order conversion with "char"

查看:170
本文介绍了具有“char”的网络字节顺序转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直被教导,如果一个整数大于一个字符,你必须解决字节排序问题。通常,我将它包装在hton [l | s]并将其转换回ntoh [l | s]。但我很困惑为什么这不适用于单字节字符。



我不知道为什么这是,并希望一个经验丰富的网络程序员帮助我解释为什么字节顺序只适用于多字节整数。



参考:



当数据从一台机器传送到另一台机器时,单个数据类型的字节必须重新排序,以与目标机器的字节顺序相对应。



但是当数据类型只包含一个字节时,没有重新排序


I've always been taught that if an integer is larger than a char, you must solve the byte ordering problem. Usually, I'll just wrap it in the hton[l|s] and convert it back with ntoh[l|s]. But I'm confused why this doesn't apply to single byte characters.

I'm sick of wondering why this is, and would love for a seasoned networks programmer to help me shed some light on why byte orderings only apply for multibyte integers.

Ref: http://beej.us/guide/bgnet/output/html/multipage/htonsman.html

解决方案

What you are looking for is endianness.

A big-endian architecture stores the bytes of a multibyte data type like so:

while a little-endian architecture stores them in reverse:

When data is transferred from one machine to another, the bytes of a single data type must be reordered to correspond with the endianness of the destination machine.

But when a data type only consists of one byte, there is nothing to reorder.

这篇关于具有“char”的网络字节顺序转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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