简体   繁体   中英

Convert Hex String to Hex Value

I have a large hex string

abcdef...

and I want to convert it to

0xab 0xcd 0xef 

Are there any functions that do that?

Also could you tell me what I means when people ask are those inputs in ASCII or not? abcdef are represented as a string. Not sure if that is ASCII or not. not sure what they mean. I am very new to programming so help here would be appreciated. I have a huge string that I need to use in my array and converting it into the aforementioned format will help me initialize my array with the hex string.

Read in each character one by one and convert it to a hex value (which is pretty easy).

You then need to, before reading the next number multiply the value by 16 (or, indeed, shift it left by 4) and read the next digit before adding it to the number you have so far. Keep going until you reach the end of your string.

When someone asks if they inputs are ASCII they are referring to whether your hex string is encoded using ASCII encoding. There are, equally various other encoding methods that range from the obsolete EBCDIC to the far more modern Unicode (which has different encodings which are still all unicode).

Bear in mind that the numbers 0 to 9, a to f and A to F have ASCII (or indeed unicode) values that are after one another in the encoding. So for numbers you can calculate its REAL value by doing "character - '0'". For 0 this will give you 0 and up to 9 it will give you 9 ...

the kit of parts you need in C is

for (..,i+=2)

and

strtoul(..,16)

You can do this using string streams.

#include <iostream>
#include <sstream>
#include <vector>

int main( int , char ** )
{
  const char *str = "AFAB2591CFB70E77C7C417D8C389507A5";
  const char *p1  = str;
  const char *p2  = p1;

  std::vector<unsigned short> output;

  while( *p2 != NULL ) {
    unsigned short byte;

    ++p2;
    if( *p2 != NULL ) {
      ++p2;
    }

    std::stringstream sstm( std::string( p1, p2 ) );

    sstm.flags( std::ios_base::hex );
    sstm >> byte;

    output.push_back( byte );

    p1 += 2;
  }

  for( std::vector<unsigned short>::const_iterator it = output.begin(); it != output.end(); ++it ) {
    std::cout << std::hex << std::showbase << *it << "\t" << std::dec << std::noshowbase << *it << "\n";
  }

  std::cout << "Press any key to continue ...";
  std::cin.get();
}

Note that if you use unsigned char instead of unsigned short the conversion from stringstream attempts to convert it into an ASCII character and it doesn't work.

Approximately the following (I wish I could made it shorter and use some library functions, any ideas?):

The function string_to_vector takes a character string and its length as input. It goes over the string, processing two characters ( str[ i ] and str[ i + 1 ] ) at a time. (For odd values of n , the last pass process only one character ( str[ i ] though.) Each character is converted to numeric value using the hex_char_to_int method. Then it constructs a number by "joining" the two numeric values by shifting and adding. Finally, the constructed numeric value is appended to a vector of numeric values which is returned at the end of the function.

std::vector< unsigned >
string_to_vector( const char * str, size_t n ) {
    std::vector< unsigned > result;
    for( size_t i = 0; i < n; i += 2 ) {
        unsigned number = hex_char_to_int( str[ i ] ); // most signifcnt nibble
        if( (i + 1) < n ) {
            unsigned lsn = hex_char_to_int( str[ i + 1 ] ); // least signt nibble
            number = (number << 4) + lsn;
        }
        result.push_back( number );
    }
    return result;
}

The following function converts characters in the range [0-9A-Za-z] to the corresponding unsigned int value.

unsigned
hex_char_to_int( char c ) {
    unsigned result = -1;
    if( ('0' <= c) && (c <= '9') ) {
        result = c - '0';
    }
    else if( ('A' <= c) && (c <= 'F') ) {
        result = 10 + c - 'A';
    }
    else if( ('a' <= c) && (c <= 'f') ) {
        result = 10 + c - 'a';
    }
    else {
        assert( 0 );
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM