将十六进制从char阵列转换为十进制

发布于 2025-01-22 18:58:05 字数 1399 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

暖风昔人 2025-01-29 18:58:05

尽管您的问题缺乏一些信息,但我试图为我所理解的内容提供答案。

#include <stdio.h>
#include <stdint.h>

#define NUM_VALUES 5
int main(void)
{
  uint8_t input_data[NUM_VALUES * 2] = {1, 2, 0x0d, 0x60, 5, 6, 7, 8, 9, 10};
  uint16_t result_data[NUM_VALUES];

  for (int i = 0; i < NUM_VALUES; i++)
  {
    result_data[i] = (uint16_t)input_data[2*i] << 8 | input_data[2*i+1];
    printf("result[%d] = 0x%04x == %5u\n", i, result_data[i], result_data[i]);
  }
}

输出:

~/tests$ ./test
result[0] = 0x0102 ==   258
result[1] = 0x0d60 ==  3424
result[2] = 0x0506 ==  1286
result[3] = 0x0708 ==  1800
result[4] = 0x090a ==  2314

如您所见,十六进制和十进制仅表示相同的值。

While your question lacks some information, I try to provide an answer for what I have understood.

#include <stdio.h>
#include <stdint.h>

#define NUM_VALUES 5
int main(void)
{
  uint8_t input_data[NUM_VALUES * 2] = {1, 2, 0x0d, 0x60, 5, 6, 7, 8, 9, 10};
  uint16_t result_data[NUM_VALUES];

  for (int i = 0; i < NUM_VALUES; i++)
  {
    result_data[i] = (uint16_t)input_data[2*i] << 8 | input_data[2*i+1];
    printf("result[%d] = 0x%04x == %5u\n", i, result_data[i], result_data[i]);
  }
}

Output:

~/tests$ ./test
result[0] = 0x0102 ==   258
result[1] = 0x0d60 ==  3424
result[2] = 0x0506 ==  1286
result[3] = 0x0708 ==  1800
result[4] = 0x090a ==  2314

As you can see, hex and decimal are only representation of the same value.

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