Convert array of characters into array of their binary values

I’m trying to turn the array “hello”, with the DEC values of the characters used being: 56, 53, 60, 60, 63 and the array I’m trying to therefore obtain is: “0110100001100101011011000110110001101111” (the values of each character has 8 bits, starting with 0).

What I have written so far is:

  int v[256];
  char c[] = "hello";
  int ascii_dec, int = 0, k = 0, j = 0;
  
  while (c[i] != '\0') {
    ascii_dec = c[i] - '0';
    Serial.println(ascii_dec);
    
    v[k] = 0;
    ++k;
    for (j = 7; j > 0; j--) {
      v[k] = bitRead(ascii_dec, j);
      Serial.print(" Bit is: ");
      Serial.print(v[k]);
      Serial.print(" and k is: ");
      Serial.print(k);
      Serial.print(" ---- ");
      ++k;
    }
    
    Serial.print("\n");
    
    ++i;
  }

  for (i = 0; i < 40; i++) {
    Serial.print(v[i]);
  }

I have assumed that each value is represented on 7 bits.

The output is (I have ommited printing the first ‘0’ I’m trying to add in order to obtain the value on 8 bits):

56
Bit is: 0 and k is: 1 ---- Bit is: 0 and k is: 2 ---- Bit is: 1 and k is: 3 ---- Bit is: 1 and k is: 4 ---- Bit is: 1 and k is: 5 ---- Bit is: 0 and k is: 6 ---- Bit is: 0 and k is: 7 ----
53
Bit is: 0 and k is: 9 ---- Bit is: 0 and k is: 10 ---- Bit is: 1 and k is: 11 ---- Bit is: 1 and k is: 12 ---- Bit is: 0 and k is: 13 ---- Bit is: 1 and k is: 14 ---- Bit is: 0 and k is: 15 ----
60
Bit is: 0 and k is: 17 ---- Bit is: 0 and k is: 18 ---- Bit is: 1 and k is: 19 ---- Bit is: 1 and k is: 20 ---- Bit is: 1 and k is: 21 ---- Bit is: 1 and k is: 22 ---- Bit is: 0 and k is: 23 ----
60
Bit is: 0 and k is: 25 ---- Bit is: 0 and k is: 26 ---- Bit is: 1 and k is: 27 ---- Bit is: 1 and k is: 28 ---- Bit is: 1 and k is: 29 ---- Bit is: 1 and k is: 30 ---- Bit is: 0 and k is: 31 ----
63
Bit is: 0 and k is: 33 ---- Bit is: 0 and k is: 34 ---- Bit is: 1 and k is: 35 ---- Bit is: 1 and k is: 36 ---- Bit is: 1 and k is: 37 ---- Bit is: 1 and k is: 38 ---- Bit is: 1 and k is: 39 ----
0001110000011010000111100001111000011111

So… it doesn’t seem to do what I’m trying to. Does anyone have any idea why?

Serial.print (x, BIN); ?

I'm not really clear what you're getting at - they're already in an array of their binary values.

Why the adjustment for the character zero:

    ascii_dec = c[i] - '0';

probably should be:

    ascii_dec = c[i];

laoonatic:
I have assumed that each value is represented on 7 bits.

Each byte constists of 8 bits. If you write the byte in binary, this might be:
byte x=0b00001011;

The leftmost bit is called the 7th bit, and from left to right the bit numbering is:
bit-7, bit-6, bit-5, bit-4, bit-3, bit-2, bit-1, bit-0

bit-0 stands for 20 = 1
bit-1 stands for 21 = 2
bit-2 stands for 22 = 4
bit-3 stands for 23 = 8
bit-4 stands for 24 = 16
bit-5 stands for 25 = 32
bit-6 stands for 26 = 64
bit-7 stands for 27 = 128

So 0b00001011
== 123 + 022 + 121 + 120
== 8 + 0 + 2 + 1
== 9;

So the number with value 9 (decimal) is just the same as 0b00001011 (binary).
For the controller 9 and 0b00001011 are absolutely the same - no difference!

They are not different numbers, its just a different visual representation of a number.

Is there any special reason why you want to convert the bit encoded “hello” array to a much bigger char array of ASCII-Codes “0110100001100101011011000110110001101111”?

If you want to do so, you could access any bit in the “hello” string using the bitRead() macro.

Example code:

void setup() {
  Serial.begin(9600);
  char str[]="hello";
  Serial.println();
  Serial.print(str);
  Serial.println(" converts to ");
  for (int i=0;i<strlen(str);i++)  // loop through all chars in string (from 'h' to 'o')
  {
    for (int j=7;j>=0;j--) // loop from bit-7 down to bit-0 (high-bit to low-bit)
      Serial.print(bitRead(str[i],j)); // from str[i] read the bit-j and print it
  }
  Serial.println();
}

void loop() {}

@econjack Thank you, that was indeed the problem!

@AWOL I know I can print their values like this, but I needed them stored in a vector.

@jurs I know that a byte consists of 8 bits, but the ASCII codes can be stored on 7 bits. Now I see that I overlooked the fact that I could read the bits starting from 8 and it would basically be the same. Probably because when you print a letter in BIN it gives you only 7 bits and that got stuck in my mind. And yes, there is a reason, I want to calculate a checksum for these bits. Also, I do not want to convert the bit encoded char array into another char array, but into an array of integers.

Thank you all for your answers!

I have changed my code to this:

while (c[i] != '\0') {
    
    for (j = 8; j > 0; j--) {
      v[k] = bitRead(c[i], j);
      Serial.print(" Bit is: ");
      Serial.print(v[k]);
      Serial.print(" and k is: ");
      Serial.print(k);
      Serial.print(" ---- ");
      k++;
    }
    Serial.print("\n");
    i++;
  }
  
  for (i = 0; i < 40; i++) {
    Serial.print(v[i]);
  }
  
  // check:
  Serial.print("\n");
  i = 0;
  while (c[i] != '\0') {
    Serial.print(c[i], BIN);
    i++;
  }

I added the last part in order to check my output, and the not-so-funny thing is that the outputs differ like this.

Array computed with bitRead vs. Serial.print with argument BIN (in fact there were groups of 7 bits, I added one 0 in front of them to make out the difference)

0011010000110010001101100011011000110111
0110100001100101011011000110110001101111

The array has an additional “0” at the beginning, lacks “1” in the end and has the 16th bit wrong and I have no idea why.

for (j = 8; j > 0; j--) {
      v[k] = bitRead(c[i], j);

Bit read numbers the bits 0 through 7, not 1 through 8.

It's the same thing, actually :) Whether you go from 0 (inclusive) to 7, or from 0 (exclusive) to 8 you get 8 steps.

Edit: No, it's not. My bad. I forgot that I was printing bits, not just counting steps. Thank you!

laoonatic: The array has an additional "0" at the beginning, lacks "1" in the end and has the 16th bit wrong and I have no idea why.

Perhaps you'd read my last posting once more.

A byte has 8 bits, the bit numbers go from bit-7 to bit-0 And that's what the bit index counts from left to right: 7 down to 0.

And you are doing:

for (j = 8; j > 0; j--) {
      v[k] = bitRead(c[i], j);

There is no bit-8 in a byte as bit-7 already is the 8th bit.

laoonatic: It's the same thing, actually :) Whether you go from 0 (inclusive) to 7, or from 0 (exclusive) to 8 you get 8 steps.

Except it's not the same thing because when you call bitread( 8 ) your index is out of bounds. It looks for the ninth bit in the byte and that just isn't there. Sure it is still 8 steps, but you miss one bit, pick up an extra bit that isn't there, and get the last one wrong. Wasn't that your complaint in post 5?

I was editing my post, you're right, I noticed right now. As I was saying in the edit, I forgot that I was printing bits, not just counting steps. Silly. Thank you!