5

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.

2
  • Its hard to understand what you mean, are you trying to go from char* strRep = "abcdef" to int intRep = 11259375;? or Trying to parse the string into separate bytes? Commented Sep 29, 2010 at 20:28
  • so i just have a huge string....that is actually a hex string. so something like AFAB2591CFB70E77C7C417D8C389507A5... now i want to put it in a byte array like const byte test[] {0xaf,0xab....}; so i guess i am trying to parse the large string so i can use it in my code to initialize a byte array. does that help? Commented Sep 29, 2010 at 20:31

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 ...

1 Comment

so there is no existing function that does that in c++ i would have to manually do this?
1

the kit of parts you need in C is

for (..,i+=2) 

and

strtoul(..,16) 

2 Comments

i am using c++ how do i do it in that?
@user245823: Same as in C. Most things in C work in C++, and this isn't one of the exceptions. If you have the value in a C++ string, you will have to get it with the .c_str() member function to pass to strtoul, though.
1

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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.