Using STL how do I convert a variable to a binary string

Discussion in 'C++' started by young_leaf, Jan 24, 2006.

  1. young_leaf

    young_leaf Guest

    Using STL how do I convert a variable to a binary string but with
    special case which
    for example BYTE x, i need only the first 3 bits of this byte, is that
    possible?

    to be more specific,
    from that byte i want to generate (so valid values for x will be 0-5)

    000
    001
    010
    011
    100
    101
    110 - not used
    111 - not used



    --
    yl
     
    young_leaf, Jan 24, 2006
    #1
    1. Advertising

  2. young_leaf

    JustBoo Guest

    On 24 Jan 2006 10:48:39 -0800, "young_leaf" <>
    wrote:
    >Using STL how do I convert a variable to a binary string but with
    >special case which
    >for example BYTE x, i need only the first 3 bits of this byte, is that
    >possible?


    std::bitset

    I quite like it. You can manipulate each bit in a simple way.

    HTH - Good Luck
     
    JustBoo, Jan 24, 2006
    #2
    1. Advertising

  3. young_leaf wrote:
    > Using STL how do I convert a variable to a binary string but with
    > special case which
    > for example BYTE x, i need only the first 3 bits of this byte, is that
    > possible?


    Everything is possible.

    Use 'ostringstream'. Write two functions: one to convert 'BYTE', and the
    other to convert everything else. The one that converts 'BYTE' should
    also check the range.

    > to be more specific, [...]


    Yeah, yeah, we got that.

    V
     
    Victor Bazarov, Jan 24, 2006
    #3
  4. young_leaf wrote:
    > Using STL how do I convert a variable to a binary string but with
    > special case which
    > for example BYTE x, i need only the first 3 bits of this byte, is that
    > possible?
    >
    > to be more specific,
    > from that byte i want to generate (so valid values for x will be 0-5)
    >
    > 000
    > 001
    > 010
    > 011
    > 100
    > 101
    > 110 - not used
    > 111 - not used


    If by "STL," you mean the C++ standard library, then I think a
    std::bitset does what you need, and specifically its to_string() member
    function. You'll need to mask or reset the bits you don't need.

    Best regards,

    Tom
     
    Thomas Tutone, Jan 24, 2006
    #4
  5. young_leaf

    Mike Wahler Guest

    "young_leaf" <> wrote in message
    news:...
    > Using STL how do I convert a variable to a binary string but with
    > special case which
    > for example BYTE x, i need only the first 3 bits of this byte, is that
    > possible?
    >
    > to be more specific,
    > from that byte i want to generate (so valid values for x will be 0-5)
    >
    > 000
    > 001
    > 010
    > 011
    > 100
    > 101
    > 110 - not used
    > 111 - not used


    #include <bitset>
    #include <climits>
    #include <iostream>
    #include <string>

    unsigned int bin_digits(unsigned int i)
    {
    unsigned int result(1); /* count 0 as 1 bit */

    while(i--)
    {
    i /= 2;
    ++result;
    }

    return result;
    }

    std::string gen(unsigned int x, unsigned int mx = 5)
    {
    std::bitset<sizeof x * CHAR_BIT> bs(x);
    std::string s(bs.to_string());

    return (x <= mx) ? s.substr(s.size() - bin_digits(mx))
    : "out of range";
    }

    int main()
    {
    for(int i = 0; i < 10; ++i)
    std::cout << i << " : " << gen(i) << '\n';

    return 0;
    }

    Output:

    0 : 000
    1 : 001
    2 : 010
    3 : 011
    4 : 100
    5 : 101
    6 : out of range
    7 : out of range
    8 : out of range
    9 : out of range


    -Mike
     
    Mike Wahler, Jan 24, 2006
    #5
  6. young_leaf

    Pete Becker Guest

    Mike Wahler wrote:
    >
    > #include <bitset>
    > #include <climits>
    > #include <iostream>
    > #include <string>
    >
    > unsigned int bin_digits(unsigned int i)
    > {
    > unsigned int result(1); /* count 0 as 1 bit */
    >
    > while(i--)
    > {
    > i /= 2;
    > ++result;
    > }
    >
    > return result;
    > }
    >
    > std::string gen(unsigned int x, unsigned int mx = 5)
    > {
    > std::bitset<sizeof x * CHAR_BIT> bs(x);
    > std::string s(bs.to_string());
    >
    > return (x <= mx) ? s.substr(s.size() - bin_digits(mx))
    > : "out of range";
    > }
    >
    > int main()
    > {
    > for(int i = 0; i < 10; ++i)
    > std::cout << i << " : " << gen(i) << '\n';
    >
    > return 0;
    > }
    >


    Seems awfully complicated for such a simple transformation.

    #include <string>
    #include <iostream>

    std::string to_binary(unsigned value)
    {
    if (5 < value)
    return "out of range";
    char res[4];
    res[3] = '\0';
    res[2] = (value & 0x01) ? '1' : '0';
    res[1] = (value & 0x02) ? '1' : '0';
    res[0] = (value & 0x04) ? '1' : '0';
    return res;
    }

    int main()
    {
    for(int i = 0; i < 10; ++i)
    std::cout << i << " : " << to_binary(i) << '\n';

    return 0;
    }

    Here's one that's even simpler:

    char *binary[7] =
    { "000", "001", "010", "011", "100", "101", "out of range" };

    std::string to_binary(unsigned value)
    {
    if (6 < value)
    value = 6;
    return binary[value];
    }

    Of course, neither of these honors the artificial constraint that the
    code should use STL, if "STL" means "Standard Template Library".

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Jan 24, 2006
    #6
  7. young_leaf

    Guest

    Pete Becker wrote:

    > Of course, neither of these honors the artificial constraint that the
    > code should use STL, if "STL" means "Standard Template Library".


    You could always do something like this:

    std::vector<int> v;
    int b = 42;
    do
    {
    v.push_back(i & 1 ? 1:0);
    }
    while (b >> 1)

    then...
    std::eek:stringstream out;
    std::copy(v.begin(), v.end(), ostream_iterator(out));

    std::string result = out.str();

    Yeah, it might be about the most inefficient way you could possibly do
    it (ok, I could probably come up with worst) but at least it uses the
    STL. ;)
     
    , Jan 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Connection String object Convert to String Variable Type

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,069
    Norman Yuan
    Oct 26, 2004
  2. Matej Barac

    Convert string to binary

    Matej Barac, Jan 31, 2005, in forum: C++
    Replies:
    4
    Views:
    33,204
    David Harmon
    Jan 31, 2005
  3. Achim Domma
    Replies:
    3
    Views:
    617
    Sjoerd Mullender
    Feb 17, 2004
  4. Harlin Seritt
    Replies:
    29
    Views:
    1,187
    Paul Rubin
    Feb 24, 2007
  5. gerg
    Replies:
    6
    Views:
    17,040
Loading...

Share This Page