How to pack a hexadecimal value in an unsigned char variable in a C program?

瘦欲@ 提交于 2019-12-11 03:14:33

问题


I have a hexadecimal value "F69CF355B6231FDBD91EB1E22B61EA1F" in a string and I am using this value in my program by hardcoding the value in an unsigned char variable like this: unsigned char a[] = { 0xF6 ,0x9C ,0xF3 ,0x55 ,0xB6 ,0x23 ,0x1F ,0xDB ,0xD9 ,0x1E ,0xB1 ,0xE2 ,0x2B ,0x61 ,0xEA ,0x1F}; Is there any function or any other method by which I can take the value from a string and put it into an unsigned variable in the hexadecimal format by packing it?


回答1:


#include <stdio.h>
#include <ctype.h>

int hctoi(const char h){
    if(isdigit(h))
        return h - '0';
    else
        return toupper(h) - 'A' + 10;
}

int main(void){
    const char cdata[]="F69CF355B6231FDBD91EB1E22B61EA1F";
    unsigned char udata[(sizeof(cdata)-1)/2];
    const char *p;
    unsigned char *up;

    for(p=cdata,up=udata;*p;p+=2,++up){
        *up = hctoi(p[0])*16 + hctoi(p[1]);
    }

    {   //check code
        int i;
        for(i=0;i<sizeof(udata);++i)
            printf("%02X", udata[i]);
    }
    return 0;
}



回答2:


You can translate a hexadecimal value in a string into a value using sscanf. If you want an array of values, then you could write a function to split up the input string into two character segments and use sscanf to convert each piece. (I haven't done C for an eternity so I don't know if that's a good way to do it.)




回答3:


If it's for 32 single hex-values (16 bytes, 128bit) only then you might take look at the methods provided by libuuid.

libuuid is part of the e2fsprogs package. Anyhow some linux distros, Debian for example, ship libuuid as a separate package. To use the Debian package for your developement you also need to look here.




回答4:


Check this answer for doing this stuff in c++ using sscanf().

For c, it would be something like this:

char *str           = "F69CF355B6231FDBD91EB1E22B61EA1F";
char substr[3]      = "__";    
unsigned char *a    = NULL;    
len                 = strlen(str);
a                   = malloc(sizeof(unsigned char)*(len/2)+1);
for (  i = 0; i < len/2; i++) {
    substr[0]       = str[i*2];
    substr[1]       = str[i*2 + 1];
    sscanf( substr, "%hx", &a[i] );
}
free(a);



回答5:


Introduce auxiliary functions data_length and data_get to easily iterate over your data. The following program dumps unpacked unsigned chars on stdout, one per line:

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

/* htoi(H)
    Return the value associated to the hexadecimal digit H. */
int
htoi(char h)
{
  int a = -1;
  if(isdigit(h))
  {
    a = h - '0';
  }
  else
  {
    a = toupper(h) - 'A' + 10;
  }
  return a;
}

/* data_length(D)
    The length of the data stored at D. */
int
data_length(const char* d)
{
  return strlen(d) / 2;
}

/* data_get(D, K)
    Return the K-th unsigned char located encoded in d. */
unsigned char
data_get(const char *d, int k)
{
  return htoi(d[2*k]) * 0x10 +
    htoi((d+1)[2*k]);
}

int
main()
{
    const char cdata[]="F69CF355B6231FDBD91EB1E22B61EA1F";
    for(int i = 0; i < data_length(cdata); ++i)
    {
      printf("0x%02hhx\n", data_get(cdata, i));
    }
    return EXIT_SUCCESS;
}


来源:https://stackoverflow.com/questions/10653499/how-to-pack-a-hexadecimal-value-in-an-unsigned-char-variable-in-a-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!