473,394 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Jan 24 '06 #1
6 2551
On 24 Jan 2006 10:48:39 -0800, "young_leaf" <ke***********@gmail.com>
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
Jan 24 '06 #2
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

Jan 24 '06 #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
Jan 24 '06 #4

"young_leaf" <ke***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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
Jan 24 '06 #5
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)
Jan 24 '06 #6

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

Jan 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
2
by: RP2001 | last post by:
I've serialized in various variables of various types (mainly CString, int, double) into an instantiated MFC CArchive class and saved it as a binary file. I am able to open the binary file and...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
3
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British...
5
by: Anil Gupte | last post by:
I am learning how to read and write files. Text files were easy, but binary files have me confused. As per the tutorial, I used BinaryReader to read a file and put the contents into a ByteArray:...
2
by: sypi | last post by:
Hi, I'd need to construnct an EXEC statement in my script I am pretty much stuck with data conversion. I need to compare a binary variable with a SID value to an actual SID found in ..sysusers. ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
5
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I am having a problem converting string to binary and I am hoping someone can help me out I have a sql query that does an update that updates a binary field calles password ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.