hi - Xwindows
This is a discussion on hi - Xwindows ; i m trying to convert bcd to hex in c++.
does anybody have any idea about this?...
i m trying to convert bcd to hex in c++.
does anybody have any idea about this?
In comp.windows.x, firstname.lastname@example.org
on 3 Mar 2007 23:40:33 -0800
> i m trying to convert bcd to hex in c++.
> does anybody have any idea about this?
You're not quite in the right newsgroup, but here's a thought for you.
static char hexcodes = "0123456789ABCDEF";
std::string bcdtohex(std::string const & inp)
for(unsigned i = 0; i < inp.size(); i++)
oss << hexcodes[ (inp[i] & 0xf0) >> 4];
oss << hexcodes[ (inp[i] & 0x0f)];
int main(int argc, char **argv)
hexstr << (char) 0x01;
hexstr << (char) 0x23;
hexstr << (char) 0x45;
hexstr << (char) 0x67;
hexstr << (char) 0x98;
hexstr << (char) 0x00;
std::cout << bcdtohex(hexstr.str()) << std::endl;
The output will of course be 012345679800, followed by
a newline. (The transposition 98 is intentional, as is
the ending 00.)
Stringstreams can be very useful. :-) In this case, I'm using the one
in bcdtohex() to solve the problem you've posted; the one in main I
use to set up a test case.
To be fair, this solves what might be a slightly different problem, as
there is a distinction between:
 converting the digits of a BCD representation to printable output.
 converting the digits of a BCD representation to an arbitrarily-long
integer, converting that integer to base-16 printable output.
The latter requires multiple precision techniques.
What I've done is actually a little more general; it can handle
any sequence of bytes, not just BCD-encoded digits.
Crossposted and followups set to a slightly more logical group.
Linux. Because it's there and it works.
Windows. It's there, but does it work?
Posted via a free Usenet account from http://www.teranews.com