C: 数组的 4 个元素为 DWORD

标签 c arrays char dword

现在我可以将数组 (char*data) 中的 4 个字节(已知起始位置,不是 0)复制到 DWORD 中吗?

这有两个部分:首先,从指定位置的数组中获取 4 个字节(4 个元素,每个元素是一个 char,在 C 中为 1 个字节),然后将它们移动到 DWORD。

我也不知道该怎么做。

谢谢!

最佳答案

您可以使用memcpy(它需要string.h header):

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

int main(void)
{
    uint8_t array[] = {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
    }
    uint32_t dword;
    size_t offset = 2;

    memcpy(&dword, array + offset, sizeof(dword));
    printf("Dword value = 0x%08x\n", dword);

    return 0;
 }

关于C: 数组的 4 个元素为 DWORD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20900172/

相关文章:

c - 以毫秒和微秒精度测量 POSIX 中耗时的最佳方法是什么?

c - 定义一个宏来交换符号

java - 线程中的异常 "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException : 0 when trying to compare a string to a string array in Hangman game

Java - char、int 转换

python - 将 Django 的模板引擎移植到 C

c - Unix 域套接字错误,sendmsg : No buffer space available

c++ - 如何在 C++ 中将文本文件传输到二维数组?

c - 使用用户定义的函数用随机数/错误填充数组

c++ - C 标准 : Character set and string encoding specification

c - 需要从数据文件中读取一个字符数组,我有一个循环设置,它只输出数字而不是我期望的字符