C语言 如何使用 C 将长值(32 位)拆分为四个字符变量(8 位)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2747219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:19:53  来源:igfitidea点击:

How do I split up a long value (32 bits) into four char variables (8bits) using C?

ccharacterlong-integer

提问by PICyourBrain

I have a 32 bit long variable, CurrentPosition, that I want to split up into 4, 8bit characters. How would I do that most efficiently in C? I am working with an 8bit MCU, 8051 architectecture.

我有一个 32 位长变量 CurrentPosition,我想将其拆分为 4 个 8 位字符。我如何在 C 中最有效地做到这一点?我正在使用 8 位 MCU,8051 架构。

unsigned long CurrentPosition = 7654321;
unsigned char CP1 = 0;
unsigned char CP2 = 0;
unsigned char CP3 = 0;
unsigned char CP4 = 0;
// What do I do next? 

Should I just reference the starting address of CurrentPosition with a pointer and then add 8 two that address four times?

我是否应该只用指针引用 CurrentPosition 的起始地址,然后将该地址的 8 和 2 相加四次?

It is little Endian.

它是小端。

ALSO I want CurrentPosition to remain unchanged.

我还希望 CurrentPosition 保持不变。

回答by nos

    CP1 = (CurrentPosition & 0xff000000UL) >> 24;
    CP2 = (CurrentPosition & 0x00ff0000UL) >> 16;
    CP3 = (CurrentPosition & 0x0000ff00UL) >>  8;
    CP4 = (CurrentPosition & 0x000000ffUL)      ;

You could access the bytes through a pointer as well,

您也可以通过指针访问字节,

unsigned char *p = (unsigned char*)&CurrentPosition;
//use p[0],p[1],p[2],p[3] to access the bytes.

回答by Justin Muller

I think you should consider using a union:

我认为您应该考虑使用联合:

union {
   unsigned long position;
   unsigned char bytes[4];
} CurrentPosition;

CurrentPosition.position = 7654321;

The bytes can now be accessed as: CurrentPosition.bytes[0], ..., CurrentPosition.bytes[3]

现在可以按以下方式访问字节: CurrentPosition.bytes[0], ..., CurrentPosition.bytes[3]

回答by Maciej Hehl

If You are using an 8 bit MCU shifting a whole 32 bit variable is a bit of work. In this case it's better to read 4 bytes of CurrentPosition using pointer arithmetic. The cast:

如果您使用的是 8 位 MCU,则移位整个 32 位变量需要一些工作。在这种情况下,最好使用指针算法读取 4 个字节的 CurrentPosition。演员阵容:

unsigned char *p = (unsigned char*)&CurrentPosition;

doesn't change the CurrentPosition, but if You try to write to p[0] You will change the least significant byte of the CurrentPosition. If You want a copy do this:

不会更改 CurrentPosition,但如果您尝试写入 p[0],您将更改 CurrentPosition 的最低有效字节。如果您想要副本,请执行以下操作:

unsigned char *p = (unsigned char*)&CurrentPosition;
unsigned char arr[4];
arr[0] = p[0];
arr[1] = p[1];
arr[2] = p[2];
arr[3] = p[3];

and work with arr. (If you want most significant byte first change the order in those assignments).

并与 arr 一起工作。(如果您希望最重要的字节首先更改这些分配中的顺序)。

If You prefer 4 variables You can obviously do:

如果您更喜欢 4 个变量,您显然可以这样做:

unsigned char CP1 = p[0];
unsigned char CP2 = p[1];
unsigned char CP3 = p[2];
unsigned char CP4 = p[3];

回答by Matt Greer

CP1 = (unsigned char)(CurrentPosition & 0xFF);
CurrentPosition >>= 8;
CP2 = (unsigned char)(CurrentPosition & 0xFF);
...

回答by Matt Joiner

unsigned char *CP = &CurrentPosition;

Now CPn per your original code is accessed via CP[n].

现在,您的原始代码的 CPn 可通过CP[n].

回答by Sean

I know this was posted some time ago. But for anyone still reading the thread: Many people take the approach of sequentially shifting the original value. Why not let the compiler do the work for you. Use a union & to allow you to store the values in the same location. Define a union consisting of both a 32 bit long variable (this will be where you save your CurrentPosition) and a structure consisting of 4 char variables. Or just a simple 8 bit integer array. When you write your CurrentPosition to the long variable, it will be stored in the same location accessed when you read the 4 char variables. This method is much less labour intensive and does not allows the compiler to do the work instead of wasting time & resources.

我知道这是前一段时间发布的。但是对于仍在阅读该线程的任何人:许多人采用顺序移动原始值的方法。为什么不让编译器为您完成工作。使用联合 & 允许您将值存储在同一位置。定义一个由 32 位长变量(这将是您保存 CurrentPosition 的位置)和由 4 个字符变量组成的结构组成的联合。或者只是一个简单的 8 位整数数组。当您将 CurrentPosition 写入 long 变量时,它将存储在您读取 4 个字符变量时访问的同一位置。这种方法的劳动强度要小得多,并且不允许编译器完成工作,而不是浪费时间和资源。