How to convert a string of '11001100' into an hex 'CC'

Hi all,

I consider myself still as a beginner, but I'm progressing on C programming... as fast as a french guy can.
I'm back with a new challenge for the community. I collect some binary values into a string. Then, I need to convert this into an Hex value..... It's now few hours / nights that I'm surfing on the web to test different things, but nothing works.

Just to tell you the story. I received a signal from a RFID reader. This signal is a 64 bits. I need to play with this string of bit to mix it differently and then convert this into hex value.
Well, if one of you can help me a bit...

Thanks thanks thanks

Regis

Where is your code?

Hint: strtoul().

Thanks Pauls for a so fast answer.

Well already tried Strtoul() but certrainly in a wrong way. Here is part of the code (without the modifications on the string)

  if (bitCount > 0 && flagDone) {
    unsigned char i;
    
    Serial.print("Read ");
    Serial.print(bitCount);
    Serial.print(" bits. ");
    
    if (bitCount == 64)
    {

      for (i=0; i<64; i++)
      {
         cardCode <<=1;
         Cardcsn[i] |= databits[i];
         
         CardcsnString += String(Cardcsn[i]);
           
         Serial.print (Cardcsn[i]);
         

      }
      
      SendHex();  
    }
    
    
    else {
      // you can add other formats if you want!
     Serial.println("Unable to decode."); 
    }

     // cleanup and get ready for the next card
     bitCount = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++) 
     {
       databits[i] = 0;
     }
  }
}

void SendHex()
{
      // I really hope you can figure out what this function does
      Serial.println("csn = ");
      Serial.print(CardcsnString);
}
         CardcsnString += String(Cardcsn[i]);

So, you lied. You don't have a string. You have a String. They are not the same thing at all. The strtoul() function will work when you have a string.

Quit using Strings.

I'm guessing you don't need strings, either.

Why use Strings or strings for bit manipulation? That's like going from Boston to New York by way of Chicago.

Use "long long" (i.e. 64-bit) integers, and bitwise operators.

See: Bitwise Operators in C and C++ - Cprogramming.com

You're so right. Well, the main objective of this is to take the 8 octets of a 64 bits and put these in a reverse order.

Example : what is read when using a wiegand reader :
0101111001001111001100100000000111110001111111100010010111000000

If I split into octet
00101111 00100111 10011001 00000000 11111000 11111111 00010010 11100000
And what I need to have
11100000 00010010 11111111 11111000 00000000 10011001 00100111 00101111
This last chains is to be converted into Hex.
I don't know anything about bitwise operator as of today, but I will read carefully your link and see if I can find a solution for this issue I got.

Thanks for the tip

Try something like this.
(Warning: I did not test this code!)

unsigned long long x;
unsigned long long y;

// this reverses bytes of x and puts the result in y
// warning: this will destroy (zero out) x in the process 
y = 0ULL;
for (byte i = 1; i <= 8; i++) {
  y <<= 8; // make room for the next eight bits
  y |= (x & 255ULL); // copy the last eight bits from x to y
  x >>= 8; // drop the last eight bits from x
}

It's hard to tell what's going on just from that snippet.

What format are you receiving the data in? Are you getting an array of ascii characters '0' and '1', or an array of byte values 0 and 1, or what?

In what format do you want the data to end up? Are you trying to produce a hexadecimal representation in an ascii string, or a byte value, or what?

Preferably answer in terms of C++ data types.

Well, to make it simple, I attache the entire code.

Hope this is answering the last questions. I'm testing odometer code.

#include <stdlib.h>

#define MAX_BITS 100
#define WEIGAND_WAIT_TIME  3000 

unsigned char databits[MAX_BITS];
unsigned char bitCount;
unsigned char flagDone;
unsigned int weigand_counter;

unsigned long facilityCode=0;
unsigned long long cardCode=0;
char Cardcsn[64];
String CardcsnString;
long CardscsnHex;


void ISR_INT0()
{
  //Serial.print("0");
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
  
}


void ISR_INT1()
{
  //Serial.print("1");
  databits[bitCount] = 1;
  bitCount++;
  flagDone = 0;
  weigand_counter = WEIGAND_WAIT_TIME;  
}

void setup()
{
  pinMode(13, OUTPUT);  // LED
  pinMode(2, INPUT);     // DATA0 (INT0)
  pinMode(3, INPUT);     // DATA1 (INT1)
  
  Serial.begin(9600);
  Serial.println("RFID Readers");
  
  attachInterrupt(0, ISR_INT0, RISING);  
  attachInterrupt(1, ISR_INT1, RISING);
  

  weigand_counter = WEIGAND_WAIT_TIME;
}

void loop()
{
  if (!flagDone) {
    if (--weigand_counter == 0)
      flagDone = 1;	
  }
  
  if (bitCount > 0 && flagDone) {
    unsigned char i;
    
    Serial.print("Read ");
    Serial.print(bitCount);
    Serial.println(" bits. ");
    
    if (bitCount == 64)
    {

      for (i=0; i<64; i++)
      {
         cardCode <<=1;
         Cardcsn[i] |= databits[i];
         
   //      CardcsnString +=String(Cardcsn[i]);
           
         Serial.print (Cardcsn[i]);
         

      }
      
      SendHex();  
    }
    
    
    else {
      
     Serial.println("Unable to decode."); 
    }

     // cleanup and get ready for the next card
     bitCount = 0;
     cardCode = 0;
     for (i=0; i<MAX_BITS; i++) 
     {
       databits[i] = 0;
     }
  }
}

void SendHex()
{
      CardscsnHex = strtoul(Cardcsn, NULL, 16);
      Serial.println();
      Serial.println("csn = ");
      Serial.print(CardscsnHex);

}

So then, I receive the data in a long, want to reverse it as explained, convert it in an hex value. This will be exported in an Ascii chain.
This last chain will be sent through an http request to a server as a variable.
I know how to do the last part., But wokring on type conversion is something new for me.

Lot of things for an end of week (a chance that this monday is a day off in France)

RGeeze:
So then, I receive the data in a long, want to reverse it as explained, convert it in an hex value. This will be exported in an Ascii chain.

I assume 'chain' means 'string'.

I may have misunderstood what you mean by 'reverse', but here's an example showing how you could convert the long value 0x12345678 to a hex string "0x78563412".

void setup()
{
  Serial.begin(9600);
  long input = 0x12345678;
  char output[12];
  byte *b = (byte *)&input;
  snprintf(output, sizeof(output), "0x%02x%02x%02x%02x", (int)b[0], (int)b[1], (int)b[2], (int)b[3]);
  Serial.println(output);
}

void loop()
{
}

output:
0x78563412

The datatype "long" can only hold 32 bits.

If you need 64 bits: use "long long" or "unsigned long long": each of these holds exactly 64 bits.

You need to be very clear about what you want for " hex 'CC' "

Do you want an unsigned byte with the binary number equivalent to 221 decimal in it ? 11011101

Or do you want two character bytes with the actual characters 'C' and 'C' in them ?