Purpose: Conversion of a binary number into separate binary numbers representing digits of the decimal number. (this example is for 8-bits, other sizes follow the same pattern).
I'm to stupid right now to solve this problem...
I get a BCD number (every digit is an own 4Bit representation)
For example, what I want:
Output: BCD 0x415
Input: 0x202
What have I tried:
But I know that's very wrong this would be 202 in Bit representation and then split into 5 nibbles and then represented as decimal number again
I can solve the problem on paper but I just cant get it in a simple C-Code
You got it the wrong way round. Your code is converting from BCD to binary, just as your question's (original) title says. But the input and output values you provided are correct only if you convert from binary to BCD. In that case, try:
Proof: http://ideone.com/R0reQh
Try the following.
Also one may try the following variant (although maybe little inefficient)
The real problem here is confusion of bases and units
The 202 should be HEX which equates to 514 decimal... and therefore the BCD calcs are correct
Binary code decimal will convert the decimal (514) into three nibble sized fields: - 5 = 0101 - 1 = 0001 - 4 = 0100
The bigger problem was that you have the title the wrong way around, and you are converting Uint to BCD, whereas the title asked for BCD to Unint
This is the solution that I developed and works great for embedded systems, like Microchip PIC microcontrollers:
I am looking a way for convert a string to BCD equivalent. I use java, but it is not a question of the language indeed. I am trying to understand step by step how to convert a string to BCD.
For example, suppose i have the following string;
In BCD (according this page: http://es.wikipedia.org/wiki/Decimal_codificado_en_binario):
Ok , i think the conversion is correct but i have to save this in a byte[2]. What Should i have to do? After, i have to read the BCD and convert it to the original string '0200' but first i have to resolve String to BCD.
Regards!
Find a utility class to do this for you. Surely someone out there has written a BCD conversion utility for Java.
Here you go. I Googled 'BCD Java' and got this as the first result. Copying code here for future reference.
There's also this question: Java code or lib to decode a binary-coded decimal (BCD) from a String.
This image help me a lot to understand my own example.
The link to the code of neuro-sys/BCDConvert.java
The first step would be to parse the string into an int so that you have the numeric value of it. Then, get the individual digits using division and modulus, and pack each pair of digits into a byte using shift and add (or shift and or).
Alternatively, you could parse each character of the string into an int individually, and avoid using division and modulus to get the numbers, but I would prefer to parse the entire string up front so that you discover right away if the string is invalid. (If you get a NumberFormatException, or if the value is less than 0 or greater than 9999 then it is invalid.)
Finally, once you have assembled the two individual bytes, you can put them into the byte[2].