Hex to char array in C

后端 未结 11 1149
广开言路
广开言路 2020-11-30 09:04

Given a string of hex values i.e. e.g. \"0011223344\" so that\'s 0x00, 0x11 etc.

How do I add these values to a char array?

Equivalent to say:

<
11条回答
  •  孤街浪徒
    2020-11-30 10:06

    If the string is correct and no need to keep its content then i would do it this way:

    #define hex(c) ((*(c)>='a')?*(c)-'a'+10:(*(c)>='A')?*(c)-'A'+10:*(c)-'0') 
    
    void hex2char( char *to ){
      for(char *from=to; *from; from+=2) *to++=hex(from)*16+hex(from+1);
      *to=0;
    }
    

    EDIT 1: sorry, i forget to calculate with the letters A-F (a-f)

    EDIT 2: i tried to write a more pedantic code:

    #include  
    
    int xdigit( char digit ){
      int val;
           if( '0' <= digit && digit <= '9' ) val = digit -'0';
      else if( 'a' <= digit && digit <= 'f' ) val = digit -'a'+10;
      else if( 'A' <= digit && digit <= 'F' ) val = digit -'A'+10;
      else                                    val = -1;
      return val;
    }
    
    int xstr2str( char *buf, unsigned bufsize, const char *in ){
      if( !in ) return -1; // missing input string
    
      unsigned inlen=strlen(in);
      if( inlen%2 != 0 ) return -2; // hex string must even sized
    
      for( unsigned i=0; i

    Testing:

    #include  
    
    char buf[100] = "test";
    
    void test( char *buf, const char *s ){
       printf("%3i=xstr2str( \"%s\", 100, \"%s\" )\n", xstr2str( buf, 100, s ), buf, s );
    }
    
    int main(){
      test( buf,      (char*)0   );
      test( buf,      "123"      );
      test( buf,      "3x"       );
      test( (char*)0, ""         );
      test( buf,      ""         );
      test( buf,      "3C3e"     );
      test( buf,      "3c31323e" );
    
      strcpy( buf,    "616263"   ); test( buf, buf );
    }

    Result:

     -1=xstr2str( "test", 100, "(null)" )
     -2=xstr2str( "test", 100, "123" )
     -3=xstr2str( "test", 100, "3x" )
     -4=xstr2str( "(null)", 100, "" )
      1=xstr2str( "", 100, "" )
      3=xstr2str( "", 100, "3C3e" )
      5=xstr2str( "", 100, "3c31323e" )
      4=xstr2str( "abc", 100, "abc" )
    

提交回复
热议问题