如何使用 C 将一个 long 值(32 位)拆分为四个 char 变量(8 位)?
我有一个 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?
我应该用指针引用 CurrentPosition 的起始地址,然后将该地址加 8 2 次四次吗?
它是小端字节序。
另外我希望 CurrentPosition 保持不变。
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.
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?
It is little Endian.
ALSO I want CurrentPosition to remain unchanged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您也可以通过指针访问字节,
You could access the bytes through a pointer as well,
我认为您应该考虑使用联合:
现在可以通过以下方式访问字节:CurrentPosition.bytes[0],...,CurrentPosition.bytes[3]
I think you should consider using a union:
The bytes can now be accessed as: CurrentPosition.bytes[0], ..., CurrentPosition.bytes[3]
如果您使用的是 8 位 MCU,则移位整个 32 位变量需要做一些工作。在这种情况下,最好使用指针算术读取 4 个字节的 CurrentPosition。强制转换:
不会更改 CurrentPosition,但如果您尝试写入 p[0],您将更改 CurrentPosition 的最低有效字节。如果您想要一份副本,请执行以下操作:
并使用 arr. (如果您想要首先更改最高有效字节,请更改这些分配中的顺序)。
如果你更喜欢 4 个变量,你显然可以这样做:
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:
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:
and work with arr. (If you want most significant byte first change the order in those assignments).
If You prefer 4 variables You can obviously do:
现在,可以通过
CP[n]
访问原始代码的 CPn。Now CPn per your original code is accessed via
CP[n]
.我知道这是不久前发布的。但对于仍在阅读该主题的任何人:
许多人采取依次移动原始值的方法。为什么不让编译器为你做这些工作呢?
使用 union &允许您将值存储在同一位置。定义一个由 32 位长变量(这将是保存 CurrentPosition 的位置)和由 4 个 char 变量组成的结构组成的联合。或者只是一个简单的 8 位整数数组。当您将 CurrentPosition 写入 long 变量时,它将存储在读取 4 个 char 变量时访问的同一位置。这种方法的劳动强度要小得多,并且不允许编译器完成工作而不是浪费时间和精力。资源。
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.