【问题标题】:Parsing hexadecimal numbers into array将十六进制数解析为数组
【发布时间】:2012-05-28 23:30:51
【问题描述】:

我有一个变量char* etag="8a964ee2a5e88be344f36c22562a6486";

如何将其转换为byte 的数组,其中byte 实际上是typedef unsigned char byte

【问题讨论】:

  • 它已经是十六进制了,就像一个字符串。您想将其存储在哪种类型的设备中? unsigned long long 甚至有点长
  • @Jay 的答案都是 C++,但这是标记为 C
  • @Jay no - 另一个问题是针对 C++ 而不是 C
  • 我想将它存储为byte[16] 类型,其中byte 实际上是typedef unsigned char byte

标签: c


【解决方案1】:
byte bytes[16];
int i=0;
for (;i<16;++i) {
  int value;
  sscanf(etag+2*i,"%02x",&value);
  bytes[i] = value;
}

【讨论】:

  • 复制我的答案并使用稍微复杂一点的函数并不好玩。
  • @H2CO3 - 为了让他受益匪浅,他只在您发布几分钟后才发布,而他的答案与您的不同,以至于需要三分钟以上的时间才能回复您的答案进入他的,所以我不相信他确实抄袭了你。所以我给你们两个+1。希望能给你一些安慰。
【解决方案2】:

如果我理解正确,您希望将 2 个字母的十六进制字符组转换为字节,然后将这些字节存储在您的 char 数组中。试试这个:

char *str = "long hex string";

int i;
unsigned char bytes[16];
char tmp[3] = { 0 };
for(i = 0; i < 16; i++)
{
    memcpy(tmp, str + 2 * i, 2);
    bytes[i] = (unsigned char)strtol(tmp, NULL, 16);
}

【讨论】:

    【解决方案3】:

    你可能使用strtol

    下面是示例程序:

    /****************************************************************
     *
     * Purpose: To show examples of the strtol function.
     * Author:  M.J. Leslie
     * Date:    06-Nov-94
     *
     ****************************************************************/
    
    #include <stdlib.h>
    
    main()
    {
      char num[10];
    
                    /* Test a valid number      */
      strcpy(num,"13");
    
      printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL,  8));
      printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
      printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));
    
      puts("----------------------------------");
    
                    /* Test a slightly valid number
                     * Returns the same results as 
                     * above.           */
      strcpy(num, "13hzcd");
    
      printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL,  8));
      printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
      printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));
    
      puts("----------------------------------");
    
                    /* Test an invalid number
                     * Returns ZERO         */
      strcpy(num, "hzcd");
    
      printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL,  8));
      printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
      printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));
    
    
      puts("----------------------------------");
    
                    /* Test 0 base.
                     * This will look at the number 
                     * and decide the base for its self!
                     */
      strcpy(num, "13");
      printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0));
    
      strcpy(num, "013");
      printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0));
    
      strcpy(num, "0x13");
      printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0));
    
    }
    
    /****************************************************************
     *
     * Results of the program:
     *
     *  13(Oct) is 11(Dec)
     *  13(Dec) is 13(Dec)
     *  13(hex) is 19(Dec)
     *  ----------------------------------
     *  13hzcd(Oct) is 11(Dec)
     *  13hzcd(Dec) is 13(Dec)
     *  13hzcd(hex) is 19(Dec)
     *  ----------------------------------
     *  hzcd(Oct) is 0(Dec)
     *  hzcd(Dec) is 0(Dec)
     *  hzcd(hex) is 0(Dec)
     *  ----------------------------------
     *  13 is 13(Dec)
     *  013 is 11(Dec)
     *  0x13 is 19(Dec)
     *
     ****************************************************************/
    

    【讨论】:

    • 你能添加一个例子来展示如何使用它来分解成一个字节数组吗?
    猜你喜欢
    • 2022-11-16
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多