将字符从大尾数转换为小尾数 [英] Convert char from big endian to little endian in C

查看:420
本文介绍了将字符从大尾数转换为小尾数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个字符变量从大尾数转换为小尾数。

I'm trying to convert a char variable from big endian to little endian.

这里正是:

char name[12];

我知道如何在大和小字节序之间转换一个int,但是字符乱七八糟。

I know how to convert an int between big and little endian, but the char is messing me up.

我知道我必须首先将其转换为整数形式。

I know I have to convert it to integer form first, which I have.

这是我使用的:

(item.age >> 24) | ((item.age >> 8) & 0x0000ff00) | ((item.age << 8) & 0x00ff0000) | (item.age << 24);

为了转换char,我想用相同的方式,如果可能,因为这是我理解的唯一方式。

For converting the char, I'd like to do it in the same way, if possible, just because this is the only way I understand how to.

推荐答案

字节顺序只影响字节顺序。因为 char 正好是一个字节,所有字节序格式都是一样的。

Endianess only affects byte order. Since char is exactly one byte it is the same on all endianess formats.

想要查找libc中的endianess转换函数:

And as a note: you may want to look up the endianess conversion functions in libc:

htonl, htons
ntohl, ntohs

这篇关于将字符从大尾数转换为小尾数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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