I am trying to convert some binary data to decimal numbers. I succeeded to convert binary to hex, but now I want to do the same with binary to decimal. I want to use bitwise operators and NO array.
See my code from binairy to hex. The incoming data is 8 bits. This code first calculates the most significant number, and then, the least significant number. This works good, but I cannot figure it out for bin to dec
The function console_print_char
is sort of the printf function of my program.
void console_print_hex_char(unsigned char data){
unsigned char temp;
if(data){
temp = (data >> 4);
if(temp){
if(temp> 9) console_print_char(temp + ('A'- 10));
else console_print_char(temp + '0');
}
temp = data & 0x0F;
if(temp> 9) console_print_char(temp + ('A'- 10));
else console_print_char(temp + '0');
}
else{
console_print_char('0');
}
}
%
) and divide (/
) if you don't want to use library functions. Additionally, there is an algorithm that involves shifting bit by bit, and some conditional adding of 3. Unfortunately I don't have an URL at hand.