Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 24th, 2006, 06:55 PM
young_leaf
Guest
 
Posts: n/a
Default Using STL how do I convert a variable to a binary string

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

  #2  
Old January 24th, 2006, 07:15 PM
JustBoo
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string

On 24 Jan 2006 10:48:39 -0800, "young_leaf" <kerby.martino@gmail.com>
wrote:[color=blue]
>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?[/color]

std::bitset

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

HTH - Good Luck
  #3  
Old January 24th, 2006, 07:25 PM
Thomas Tutone
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string

young_leaf wrote:[color=blue]
> 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[/color]

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

  #4  
Old January 24th, 2006, 07:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string

young_leaf wrote:[color=blue]
> 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?[/color]

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.
[color=blue]
> to be more specific, [...][/color]

Yeah, yeah, we got that.

V
  #5  
Old January 24th, 2006, 07:35 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string


"young_leaf" <kerby.martino@gmail.com> wrote in message
news:1138128519.306558.216940@o13g2000cwo.googlegr oups.com...[color=blue]
> 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[/color]

#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


  #6  
Old January 24th, 2006, 08:35 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string

Mike Wahler wrote:[color=blue]
>
> #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;
> }
>[/color]

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)
  #7  
Old January 24th, 2006, 09:45 PM
roberts.noah@gmail.com
Guest
 
Posts: n/a
Default Re: Using STL how do I convert a variable to a binary string


Pete Becker wrote:
[color=blue]
> Of course, neither of these honors the artificial constraint that the
> code should use STL, if "STL" means "Standard Template Library".[/color]

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::ostringstream 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. ;)

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles