Converting BCD to chars or string

  • Thread starter Thread starter Daniel Passwater via DotNetMonster.com
  • Start date Start date
D

Daniel Passwater via DotNetMonster.com

I've got hexadecimal info coming to my C# compact framework app in BCD via serial connection. I don't need to convert the hex. I just need to read it and display it as text representations of the hex.

What I'm attempting to do is convert the first four bits of the byte to a string and then do the same with the last four bits. I'll just keep sticking them in a string until I have the complete string of text for display.

Does this sound like I'm heading in the right direction? If so, how do I get at that first four bits? Is there an easier way?

Thanks in advance for any and all help.
Daniel

_____________________
Message posted via http://www.dotnetmonster.com
 
Daniel Passwater via DotNetMonster.com said:
I've got hexadecimal info coming to my C# compact framework app in
BCD via serial connection. I don't need to convert the hex. I just
need to read it and display it as text representations of the hex.

What I'm attempting to do is convert the first four bits of the byte
to a string and then do the same with the last four bits. I'll just
keep sticking them in a string until I have the complete string of
text for display.

Does this sound like I'm heading in the right direction? If so, how
do I get at that first four bits? Is there an easier way?

Yes, that sounds basically plausible. I'd suggest using a StringBuilder
to build up the string - or if you know the number of characters you'll
end up with, just create a char array of the right size to start with.

To get the most significant bits of a byte, use
int highBits = (myByte & 0xf0) >> 4;

To get the least significant bits, use:
int lowBits = myByte & 0xf;
 
Back
Top