how to convert hex to bin

may cod, it works,but I want more simple :confused:

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_rx_start();

}

void loop()
{

if (vw_get_message(buf, &buflen))
{
int i;

for (i = 0; i <buflen; i++)
{
migebuli = char(buf*);*
_ // Serial.print(migebuli*);*_

* for(int j=0;j<4;j++)*
* {*
_ if(migebuli*=='0')
{
b[0]=0;
b[1]=0;
b[2]=0;
b[3]=0;
b[4]=b[j];*_

* }*

_ else if(migebuli*=='1')
{*_

* b[0]=0;*
* b[1]=0;*
* b[2]=0;*
* b[3]=1;*
* b[5]=b[j];*
_ bin2*=b[5];
}
else if(migebuli=='2')
{*_

* b[0]=0;*
* b[1]=0;*
* b[2]=1;*
* b[3]=0;*
* b[6]=b[j];*
_ bin2*=b[6];
// Serial.print(bin2);
}
else if(migebuli=='3')
{*_

* b[0]=0;*
* b[1]=0;*
* b[2]=1;*
* b[3]=1;*
* b[7]=b[j];*
_ bin2*=b[7];
}
else if(migebuli=='4')
{*_

* b[0]=0;*
* b[1]=1;*
* b[2]=0;*
* b[3]=0;*
* b[8]=b[j];*
_ bin2*=b[6];
}*_

_ else if(migebuli*=='5')
{*_

* b[0]=0;*
* b[1]=1;*
* b[2]=0;*
* b[3]=1;*
* b[9]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='6')
{*_

* b[0]=0;*
* b[1]=1;*
* b[2]=1;*
* b[3]=0;*
* b[10]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='7')
{*_

* b[0]=0;*
* b[1]=1;*
* b[2]=1;*
* b[3]=1;*
* b[11]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='8')
{*_

* b[0]=1;*
* b[1]=0;*
* b[2]=0;*
* b[3]=0;*
* b[12]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='9')
{*_

* b[0]=1;*
* b[1]=0;*
* b[2]=0;*
* b[3]=1;*
* b[13]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='A')
{*_

* b[0]=0;*
* b[1]=1;*
* b[2]=1;*
* b[3]=0;*
* b[14]=b[j];*
_ bin2*=b[6];
}
else if(migebuli=='B')
{*_

* b[0]=1;*
* b[1]=0;*
* b[2]=1;*
* b[3]=1;*
* b[15]=b[j];*
_ bin2*=b[15];
}
else if(migebuli=='C')
{*_

* b[0]=1;*
* b[1]=1;*
* b[2]=0;*
* b[3]=0;*
* b[16]=b[j];*
_ bin2*=b[16];
}*_

_ else if(migebuli* == 'D')
{
b[0]=1;
b[1]=1;
b[2]=0;
b[3]=1;
b[17]=b[j];
bin2=b[17];
}
else if(migebuli == 'E')
{
b[0]=1;
b[1]=1;
b[2]=1;
b[3]=0;
b[18]=b[j];*_

* }*

_ else if(migebuli*=='F')
{*_

* b[0]=1;*
* b[1]=1;*
* b[2]=1;*
* b[3]=1;*
* b[19]=b[j];*
_ bin2*=b[16];
// Serial.print(b[7]);*_

* }*

_ Serial.print(bin2*);
// Serial.print(i);*_

* }*
* //Serial.print(i);*
* }*
* Serial.println();*

* }*
* // buflen=0;*
* }*

Please post your code between [code ] tags. Can be done with the </> button.

And second, tell us what you try to achieve instead of just pasting code.

Hex string to binary?

use strtol() to convert your string to a long, base 16 meaning hex.

Then use the shift operators << to shift down each bit and populate your binary array, or mask it out. Why do you want the binary representation, is this for a bitfield being sent as a hex string?

   b[0]= (migebuli) & 1;
   b[1]=(migebuli>>1) & 1;
   b[2]=(migebuli>>2) & 1;
   b[3]=(migebuli>>3) & 1;

or

for(int i = 0; i<4;i++) b[i] = (migebuli>>i) & 1;

Or

#define extractBit(x,y) ((x>>y)&1)

Or more fixed

#define b(x) ((migebuli>>1)&1)

For example,

char k[]={'f','B',1 };
int b[10];//???

void setup() {
Serial.begin(9600);

}

void loop() {
// Should I get a :111110110001

Serial.println();

}

long value = strtol( k, NULL, 16 );
for(int i=0; i<10; i++) 
 b[i] = (value>>i) & 1;

rati:
For example,

Code tags, buddy.

Before I lock the thread.

Thank you but incorrectly relocate

For example,Gives incorrect results

char k[]={'f','b',1};

int b[10];
void setup() {
Serial.begin(9600);

}

void loop() {
for (int i=0;i<10;i++)
{

int value = strtol( k, NULL, 16 );

b = (value>>i) & 1;

Serial.print(b*);*
}
Serial.println();
}

Why have you just started a new thread?

And why, oh why, are you so insistent on not using code tags?!

The button on the top left of the editor when posting provides code tags to properly format your code!

tammytam:
And why, oh why, are you so insistent on not using code tags?!

And we ask this not out of malice.... look at your code, and you'll see it's turned italic halfway down. That's almost certainly because an index [i] has been taken as html for italics. Apart from turning italic, the [i] has disappeared from the code, so if anyone copies and pastes the code into the ide, it will be incomplete.

Often you'll see this:

digitalRead(8);

.... when it's supposed to be:

digitalRead(8);

Threads merged.
I can't see why a new thread was created.

I also can't see why it is so difficult to use [code][/code] tags.

char k[]={'f','B',1 };What is this meant to do ?

#include <VirtualWire.h>

pose,char Type.example char k[]={'f','f',1,2,4};

I would like to bin code 1111111000100100100

Fricking code tags dude!

I would like to bin code 1111111000100100100

What the heck does thins mean?

char k[]={'f','f',1,2,4};

Now what is this supposed to do ?
Note the code tags !

I actually believe I know what he's asking, and whats he's trying to convey. That array is to emulate his hex string coming in, slight issue as he's inserted some numerical values as opposed to their ascii equivalent. He then wants to convert this to an array representing that hex string in binary form. Is this right rati? Nod twice if you agree :wink: