How to pack binary to 8 bits

I have had a look through the forum and Google but cant find a soulution i understand.

Is there a way i can convert an int to binary of a set length? For example i would like to convert 123 to 00001111011. When i convert the int to binary i get 111011.

How can i add the leading zeros?

rich_330:
I have had a look through the forum and Google but cant find a soulution i understand.

Is there a way i can convert an int to binary of a set length? For example i would like to convert 123 to 00001111011. When i convert the int to binary i get 111011.

How can i add the leading zeros?

are you trying to fill an array with those values or just print them?

look at bitRead() function and see if you can't figure out how to print the leading zeroes in a function that scans the 8 bits in your byte.

Hello and welcome,

Explain what you want to do with those leading zeros, because they are useless.

guix:
Hello and welcome,

Explain what you want to do with those leading zeros, because they are useless.

printing them can be helpful if you are trying to have a nicer looking output that reads more easily:

00000001
00000010
00000100
11000010
00110000

BulldogLowell:
are you trying to fill an array with those values or just print them?

look at bitRead() function and see if you can't figure out how to print the leading zeroes in a function that scans the 8 bits in your byte.

At the moment i just want to print them but i would like to fill an array eventullay.

guix:
Hello and welcome,

Explain what you want to do with those leading zeros, because they are useless.

Eventually i would like to use the binary array to send Wiegand. So the binary will always need to be a certain length

For the moment im just printing the binary to the serial port so i can see what im doing :).

Here is my code:

String data = " ";
unsigned int cardNum = 0;
int cardSc = 0;
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(200);
}

void loop() {
  while (Serial.available()){
    data = Serial.readString();
  }
  if(data > " "){
    cardSc = data.substring(0, 3).toInt();
    cardNum = data.substring(3, 8).toInt();
    Serial.print("Site Code: "); 
    Serial.println(cardSc);
    Serial.print("Card Number: "); 
    Serial.println(cardNum);
    Serial.print("SC Binary: "); 
    Serial.println(cardSc, BIN);
    Serial.print("Card Number Binary: ");
    Serial.println(cardNum, BIN);
    data = " ";
  }
}

I cant see how to fit bitRead() in to this :frowning:

Sorry if im being a bit stupid fairly new to this...

You can use bitRead in a for loop to print each bit

for ( int8_t i = 7; i >= 0; i-- )
  Serial.print( bitRead( val, i ) );

expanded into a function:

byte myByte = B00110011;

void setup() 
{
  Serial.begin(9600);
  goPrint(myByte);
}

void loop() 
{

}

void goPrint(byte b)
{
  for(int i = 0; i < 8; i++)
  {
    Serial.print(bitRead(b, 7-i));
  }
  Serial.println();
}

rich_330:
At the moment i just want to print them but i would like to fill an array eventullay.

To print them, that's okay. Use the code of BulldogLowell.

To store them in a array, no, don't do that. You have the value. And it's already binary. Putting it into a binairy array after that is just plain useless... Just use bitRead() or a bit shift and you're done.