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?...

+ Reply to Thread
Results 1 to 2 of 2

Thread: hi

  1. hi

    i m trying to convert bcd to hex in c++.
    does anybody have any idea about this?


  2. Re: hi

    In comp.windows.x, anita.anika@gmail.com

    wrote
    on 3 Mar 2007 23:40:33 -0800
    <1172994033.298996.311260@c51g2000cwc.googlegroups. com>:
    > 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.

    ----8< >8-----

    #include
    #include
    #include

    static char hexcodes[] = "0123456789ABCDEF";

    std::string bcdtohex(std::string const & inp)
    {
    std:stringstream oss;
    for(unsigned i = 0; i < inp.size(); i++)
    {
    oss << hexcodes[ (inp[i] & 0xf0) >> 4];
    oss << hexcodes[ (inp[i] & 0x0f)];
    }

    return oss.str();
    }

    int main(int argc, char **argv)
    {
    std:stringstream hexstr;

    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;
    }

    ----8< >8-----

    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:

    [1] converting the digits of a BCD representation to printable output.

    [2] 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.

    --
    #191, ewill3@earthlink.net
    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


+ Reply to Thread