Convert bytes into a binary coded decimal (BCD) string


/ Published in: C#
Save to your folder(s)

BCD is where the hex value of a byte represents two integer values between zero and nine; e.g., 0x98 -> "98" (whereas its integer value is actually 152). The method throws an exception if non-BCD data is input.

There is actually a built-in method to do BCD: System.BitConverter.ToString. Note, though, that BitConverter only uses the endianness of the host machine, so you might need to convert to Big or Little Endian depending on the data source or destination.

Example: `BitConverter.ToString(new byte[] {0xAE, 0xD0})` gives us the string `AE-D0`.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.