# Converting a char value in binary storing bits into an array

I'm playing with my LCD display and I need to handle char values 4-bit at a time, I tried to manually convert a char value but it seems not working

``````void regbits(unsigned char cmd){
hexvals=(int)cmd;
while(cmd>0){
hexbits[i]=cmd%2;
cmd=cmd/2;
i++;
}
i=0;
}
``````

How can I store the binary value in an array so I can handle singularly every bit? Thx

Do you really need to handle every single bit? Or are 4-bit (which is a called a nibble) ok?
And do you really need to store the bits or can you process them directly?

Anyhow:

``````void charToBinaryArray(char c, uint8_t *binary_array)
{
for(uint8_t i = 0; i < 8; i++) {
binary_array[7-i] = c & (1 << i);
}
}

void setup() {
Serial.begin(9600);
uint8_t bin[8];
char c = 0b10111100;
charToBinaryArray(c, bin);
for(uint8_t i = 0; i < 8; i++) {
Serial.print(" ");
Serial.print((bin[i] == 0) ? "0" : "1");
}
}
``````

The array must be of size 8.

Yes, firstly I needed to store the bits in an array, this was the big problem, but how can I store the values in an integer type? Can I write something like this…

``````arraybin[7-i]=(int)(cmd & (1 << i));
``````

bitWrite?

davixd2002:
Yes, firstly I needed to store the bits in an array, this was the big problem, but how can I store the values in an integer type? Can I write something like this…

``````arraybin[7-i]=(int)(cmd & (1 << i));
``````

You don’t need to do this. Binary is just a different view of the same thing:

(ascii) “A”
(decimal) 65
(binary) 0100 0001
(hex) 0x41

The above are all the same.

I don't think it's the best way to treat a char value, because I need to covnert it into an integer array

davixd2002:
I don’t think it’s the best way to treat a char value, because I need to covnert it into an integer array

What is the purpose of the array. I don’t think it is necessary. By the way, uint8_t is also used as an “integer” type.

``````void charToBinaryArray(char c, int *binary_array)
{
for(int i = 0; i < 8; i++) {
binary_array[7-i] = c & (1 << i);
}
}

void setup() {
Serial.begin(9600);
int bin[8];
char c = 0b10111100;
charToBinaryArray(c, bin);
for(int i = 0; i < 8; i++) {
Serial.print(" ");
Serial.print((bin[i] == 0) ? "0" : "1");
}
}
``````

This works just fine, but is a huge waste of memory.

EDIT: Hang on, there is a slight mistake:

``````void charToBinaryArray(char c, int *binary_array)
{
for(int i = 0; i < 8; i++) {
binary_array[7-i] = ((c & (1 << i)) == 0) ? 0 : 1;
}
}

void setup() {
Serial.begin(9600);
int bin[8];
char c = 0b10111100;
charToBinaryArray(c, bin);
for(int i = 0; i < 8; i++) {
Serial.print(" ");
Serial.print(bin[i]);
}
}
``````

It gives me the following error if I try to insert the uint8_t variable type

Arduino:1.8.5 (Linux), Scheda:"Arduino/Genuino Uno"
no matching function for call to 'regbits(unsigned char&, unsigned int [8])'

davixd2002:
It gives me the following error if I try to insert the uint8_t variable type

Arduino:1.8.5 (Linux), Scheda:"Arduino/Genuino Uno"
no matching function for call to 'regbits(unsigned char&, unsigned int [8])'

LightuC:
(ascii) "A"
(decimal) 65
(binary) 0100 0001
(hex) 0x41

The above are all the same.

Sure?

(ascii) 'A'
(binary) 0b01000001

GolamMostafa:
Sure?

(ascii) 'A'
(binary) 0b01000001

I'm sorry (and pretty tired), but I can't see a difference between your and my post? Except the 0b, which I omitted, because I already said binary before. I know that is incosistent to the way I wrote the hex ...

Am I right in saying that the difference between 'A' and "A" in C is: 'A' - Means a character 1 byte (8 bits) long containing A. "A" - Means a string which is 2 bytes (16 bits) long which holds an A and a NULL character?

LightuC:

I tried changing the variable type, if it's a unint8_t type, it gives me the mentioned error, but if it's an unsigned int it doesn't gives me any problems, but there is another problem, If the parameter of the function is an hex value like: 0x00, 0x03 it doesn't work!

unint8_t

A what?

Explain what the problem is.

``````int i=0;
int hexbits[8];
char com=0x30;
void regbits(char cmd, int *arraybin){
for(i=0; i<8; i++){
arraybin[i]=(cmd & (1 << i));
}

}

regbits(com, hexbits);
for(i=0; i<8; i++){
printf("%d  ", hexbits[i]);
}
``````

The output is this:----> 0 0 0 0 16 32 0 0

That’s how binary works.

``````[code
int i=0;
int hexbits[8];
char com=0x30;
void regbits(char cmd, int *arraybin){
for(i=0; i<8; i++){
arraybin[i]=(cmd & (1 << i));
}

}

regbits(com, hexbits);
for(i=0; i<8; i++){
printf("%d  ", hexbits[i]);
}
]
``````

Yes, but I didn’t espect the following output, the LCD screen doesn’t seem to display the numbers, only the characters

Yes, this is because I made a mistake (back in post #7 I also showed the answer)

``````arraybin[i]=(cmd & (1 << i));
``````

needs to be changed to:

``````arraybin[i]=((cmd & (1 << i)) == 0) ? 0 : 1;
``````

or even better (untested though):

``````arraybin[i]= bitRead(cmd, i);
``````

However, this will result in the LSB being situated in the front of the array, essentially flipping the order of the bits. If don’t want that, then you need to either change the bitread or the array index:

``````arraybin[7-i]= bitRead(cmd, i);
``````
``````arraybin[i]= bitRead(cmd, 7-i);
``````

Nothing has changed, the last three digits are hex values 0x03, 0x05, 0x09, the forum doesn’t let me post the image, but it prints three same characters

compressjpeg.zip (495 KB)