String to Binary Representation

I have a string of length 32. After using substring command, I have chopped the string into substrings of length 4 each.

Now I have 0100 0100 1010 like strings. I just want to convert these substrings as their HEX equivalents. Like 0100 == 4
1010 == A etc.

I am unable to take the HEX equivalents of these substrings. Can anyone help me in sorting out this problem?

Hello and welcome,

Here is one efficient way to do it

char nibbleStringToHexChar( const char *s )
{
  uint8_t n = 0;

  while ( *s )
    n = ( n << 1 ) + ( *s++ == '1' );

  return ( n > 9 ) ? ( n-10 + 'A' ) : ( n + '0' );
}

Serial.println( nibbleStringToHexChar( "1100" ) );

Edit: changed char * to const char *.

Thanks for your response. However when I run your code in Arduino intact, it gives the error
"'Serial' does not name a type".

Your response is awaited.

It isn't a complete code, you can't run it. It's a function and below it, an example of how to use that function. Your job is to copy/paste the function in your own sketch, and use it wherever and however you want :wink:

Here is a complete example if you really want it..

char nibbleStringToHexChar( const char *s )
{
  uint8_t n = 0;

  while ( *s )
    n = ( n << 1 ) + ( *s++ == '1' );

  return ( n > 9 ) ? ( n-10 + 'A' ) : ( n + '0' );
}

void setup()
{
    Serial.begin( 9600 );
    Serial.println( nibbleStringToHexChar( "1100" ) );
}

void loop()
{
}

I've just tested and it outputs 'C' as expected.

O.K. I got your point. But it would be better if you could signify the presence of "1100" in Serial.println line.

Hope you understand.

But it would be better if you could signify the presence of "1100" in Serial.println line.

What does this mean? Signify how? You wrote the value to the output buffer. You know its there. What is the problem?