I feel rather foolish, but cannot seem to my head round this one. I am trying to convert base 16 numbers (no letters A-F used) to base 10 as per the following.
B16 0x47 = Dec 71. B16 0x16 = Dec 22. B16 0x30 = Dec 48. B16 0x01 = Dec 1 Etc
Anyone got a routine for doing this?. As I said none of the B16 numbers contain any hex characters, all are standard numbers. Seems so obvious, but I just have a blind spot here. Can obviously display in any format using printf, but this is not what I am after. Any advice most gratefully received.
The question isn't clear at all. Hex or decimal are only printable representations of internal data formats, that are always binary. To get a decimal representation in a string, sprintf() or itoa() C-functions can be used. If you intended a different representation than a printable string or internal binary format, you should specify it.
There are of course formats as BCD, that represent a two digit decimal number in 8 bits, but there's nothing in your post that suggests it.
remember
C Language can understand number in all format i.e. Hex, decimal, binary, octal. you just declare it but all mean same
for example
a= 0x04 ; Hex 4
a=4 ; decimal 4
a=04 ; octal 4
a=0b100 ; binary 4
Perhaps I did not explain myself properly. I am trying to take a numeric variable(no strings) that contains a binary number representation of a hex number, and convert this to another binary number representing a decimal number. These is always numeric data, A-F is never used.
e.g
Hex 0-9 would be the same in decimal 0-9 of course. No conversion.
If the hex input is 30 the decimal should come out as 48. Same with other examples in initial post.
I can obviously do a conversion table, but a formula would be a much better option. Effectivly Hex to Decimal without the letters. Hope this makes sense.
Sorry, but I didn't get the point.
0x30 is represented by the bit code 00110000, decimal 48 is represented the same bit code.
P.S.: If you are talking of BCD (binary coded decimal) representation, as I asked before, that's not a "binary number representing a decimal number" rather than a four bit binary number representing each digit.
Just don't pass a 5 digit hex number to it , or change the relevant variables to long.
DOH!!! this may be another answer, I just ran it under Lcc ..
Code:
/* Convert hex formatted input string to its Decimal
will accept any valid hex string */
int main()
{
int a;
while(1)
{
printf("Enter a hex number: ");
scanf("%x",&a); // hex formatted input string
printf("Is the same as Decimal %d\n\n",a); // dec format output
}
return 0;
}
may be able to run a variation of this, for your purpose.
It was from a snippet of code that I picked up. Looking at it again, of course it is BCD. I cannot believe I missed that. Just some days I don't have my brain in gear. Many thanks to both of you for putting me on the right track.
Just re-read my explanation, no wonder I caused confusion. Naturally 0x30 is the same as 48 etc. What I was actually trying to achieve was changing the hex number to decimal, but keeping the same value. So as the example 0x47 (01000111) would actualy become Dec 47 (00101111). It is in fact the opposite of the following.