I am using this code to convert hex to binary. In this code I have initialized a sample character array with 4 characters, but in real scenario, the code would have more than 20 characters.
So i have used a separate function to convert the HEX to binary. Another function to separate the digits. and the code returns garbage value. can anyone help me in this ?
here is the code:
char received_data[4] = {'2','A','3','8'};
int received_bits[4]= {0,0,0,0};
void setup() {
Serial.begin(9600);
}
void loop()
{
unsigned long first_byte_upper = convert_binary(received_data[1]);
int *first_bits_upper = convert_digits(first_byte_upper);
for(int x =0; x <= 3; x++)
{
received_bits[x] = *(first_bits_upper);
Serial.print(received_bits[x]);
first_bits_upper++;
}
//Serial.println("");
}
int* convert_digits(unsigned int byte_value)
{
int converted_digits[5] = {0,0,0,0,0};
int remaining_digits;
for(int y=0; y<=3; y++)
{
converted_digits[y] = byte_value % 10;
remaining_digits = byte_value / 10;
byte_value = remaining_digits;
}
}
unsigned long convert_binary(char a)
{
unsigned long converted;
if (a == '2')
{
converted = 0010;
return converted;
}
if (a == 'A')
{
converted = 1010;
return converted;
}
if (a == '3')
{
converted = 0011;
return converted;
}
if (a == '8')
{
converted = 1000;
return converted;
}
}