Handling of conversions from and to hex

后端 未结 3 1858

I want to build a function to easily convert a string containing hex code (eg. \"0ae34e\") into a string containing the equivalent ascii values and vice versa. Do I have to cut

相关标签:
3条回答
  • 2021-01-24 03:51

    If you want to use a more c++ native way, you can say

    std::string str = "0x00f34" // for example
    
    stringstream ss(str);
    
    ss << hex;
    
    int n;
    
    ss >> n;
    
    0 讨论(0)
  • 2021-01-24 03:54

    Based on binascii_unhexlify() function from Python:

    #include <cctype> // is*
    
    int to_int(int c) {
      if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found
      if (isdigit(c)) return c - '0';
      if (isupper(c)) c = tolower(c);
      return c - 'a' + 10;
    }
    
    template<class InputIterator, class OutputIterator> int
    unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
      while (first != last) {
        int top = to_int(*first++);
        int bot = to_int(*first++);
        if (top == -1 or bot == -1)
          return -1; // error
        *ascii++ = (top << 4) + bot;
      }
      return 0;
    }
    

    Example

    #include <iostream>
    
    int main() {
      char hex[] = "7B5a7D";
      size_t len = sizeof(hex) - 1; // strlen
      char ascii[len/2+1];
      ascii[len/2] = '\0';
    
      if (unhexlify(hex, hex+len, ascii) < 0) return 1; // error
      std::cout << hex << " -> " << ascii << std::endl;
    }
    

    Output

    7B5a7D -> {Z}
    

    An interesting quote from the comments in the source code:

    While I was reading dozens of programs that encode or decode the formats here (documentation? hihi:-) I have formulated Jansen's Observation:

    Programs that encode binary data in ASCII are written in such a style that they are as unreadable as possible. Devices used include unnecessary global variables, burying important tables in unrelated sourcefiles, putting functions in include files, using seemingly-descriptive variable names for different purposes, calls to empty subroutines and a host of others.

    I have attempted to break with this tradition, but I guess that that does make the performance sub-optimal. Oh well, too bad...

    Jack Jansen, CWI, July 1995.

    0 讨论(0)
  • 2021-01-24 03:55

    The sprintf and sscanf functions can already do that for you. This code is an example that should give you an idea. Please go through the function references and the safe alternatives before you use them

    #include <stdio.h>
    int main()
    {
     int i;
     char str[80]={0};
     char input[80]="0x01F1";
     int output;
     /* convert a hex input to integer in string */
     printf ("Hex number: ");
     scanf ("%x",&i);
     sprintf (str,"%d",i,i);
     printf("%s\n",str);
    /* convert input in hex to integer in string */
     sscanf(input,"%x",&output);
     printf("%d\n",output);
    }
    
    0 讨论(0)
提交回复
热议问题