如何使用 C 将一个 long 值(32 位)拆分为四个 char 变量(8 位)?

发布于 2024-08-31 06:26:38 字数 391 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彼岸花ソ最美的依靠 2024-09-07 06:26:38
    CP1 = (CurrentPosition & 0xff000000UL) >> 24;
    CP2 = (CurrentPosition & 0x00ff0000UL) >> 16;
    CP3 = (CurrentPosition & 0x0000ff00UL) >>  8;
    CP4 = (CurrentPosition & 0x000000ffUL)      ;

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

unsigned char *p = (unsigned char*)&CurrentPosition;
//use p[0],p[1],p[2],p[3] to access the bytes.
    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.
不奢求什么 2024-09-07 06:26:38

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

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

CurrentPosition.position = 7654321;

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

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]

南笙 2024-09-07 06:26:38

如果您使用的是 8 位 MCU,则移位整个 32 位变量需要做一些工作。在这种情况下,最好使用指针算术读取 4 个字节的 CurrentPosition。强制转换:

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

不会更改 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];

并使用 arr. (如果您想要首先更改最高有效字节,请更改这些分配中的顺序)。

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

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

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:

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:

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).

If You prefer 4 variables You can obviously do:

unsigned char CP1 = p[0];
unsigned char CP2 = p[1];
unsigned char CP3 = p[2];
unsigned char CP4 = p[3];
夜司空 2024-09-07 06:26:38
CP1 = (unsigned char)(CurrentPosition & 0xFF);
CurrentPosition >>= 8;
CP2 = (unsigned char)(CurrentPosition & 0xFF);
...
CP1 = (unsigned char)(CurrentPosition & 0xFF);
CurrentPosition >>= 8;
CP2 = (unsigned char)(CurrentPosition & 0xFF);
...
千笙结 2024-09-07 06:26:38
unsigned char *CP = &CurrentPosition;

现在,可以通过 CP[n] 访问原始代码的 CPn。

unsigned char *CP = &CurrentPosition;

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

娇柔作态 2024-09-07 06:26:38

我知道这是不久前发布的。但对于仍在阅读该主题的任何人:
许多人采取依次移动原始值的方法。为什么不让编译器为你做这些工作呢?
使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文