Encoding BCD to ASCII help!

Summer

New member
Joined
Jul 29, 2020
Messages
2
Programming Experience
1-3
Hello,
I need help with this encoding BCD ---> ASCII. this all two days i am tried to learning about that but didn't find anything that helps. (All is Binary to ASCII).
How to convert that in .NET Core C#.
Example:

46 (Decimal) = 101110 (Binary) = 2E (Hex) = 0100 0110 (BCD) = 34 36 (ASCII) = F4 F6 (EBCDIC)

I did make all the converts but the only is missing is - "0100 0110 (BCD) = 34 36 (ASCII)" .

Introduction to ISO 8583
 
You would get the ASCII value using Convert.ToInt32. That will actually give you a Unicode value but that will be the same as ASCII for digits. To use that, you need a char value to start with. I'd probably recommend starting with the original number, calling ToString and then looping over the char values in that. If you are starting with BCD values then you need to convert each four-bit value into a number, then into a string, then into a char.
 
string s = "01000110";
string result = "";
while (s.Length > 0)
{
var first8 = s.Substring(0,8);
s = s.Substring(8);
var number = Convert.ToInt32(first8, 2);
result += (char)number;
}
Console.WriteLine(result);


-----------------------------------------------------------------------------------------------------------
from that, I get the result of the convert: "F" ( I believe it's from binary to ASCII) not BCD
Is should be: "34 36"
 
That code doesn't make any sense. Why would you think that working on eight bits at a time was appropriate when there are obviously two sets of four bits representing the two digits? Why would you think that converting a number to a char was appropriate when the whole point of the exercise is to convert chars to numbers?

This is what happens when you try to write code without knowing what that code has to do, i.e. the steps to get to a result rather than just the result. Put the code away for now and do the process manually. Think about the steps that need to be performed and perform them. Formalise those steps into an algorithm and follow that algorithm. If it works for a manual process then it will work in code, so you can then write code to implement the algorithm specifically. At every step, you can compare the code to the algorithm to confirm that it does what it is supposed to do. Only when you have a working algorithm and code that you believe implements that algorithm but doesn't produce the correct result do you need to post here.
 
Last edited:
Back
Top Bottom