Hi,
I am trying this in my code and getting strange answers. Would
appreciate if someone can point out what it is i'm missing here. My
thanks in advance.
-------------code snippets--------------
// where str is a std::string variable
std::bitset<4> size;
size.reset();
size |= std::atoi( (str.substr(pos,4)).c_str() );
std::cout << "next - <" << str.substr(pos,4) << ">, after convert - "
<< size.template to_string<char,
std::char_traits<char>,std::allocator<char> >();
------------output to console-----------
size - <0110>, after convert - 1110
----------end of output---------------
Above is the output i get. Notice i am expecting after convert to show
0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003.
Hmmmm..... 5 1843
DamonChong wrote: Hi,
I am trying this in my code and getting strange answers. Would appreciate if someone can point out what it is i'm missing here. My thanks in advance.
-------------code snippets-------------- // where str is a std::string variable std::bitset<4> size; size.reset(); size |= std::atoi( (str.substr(pos,4)).c_str() );
std::cout << "next - <" << str.substr(pos,4) << ">, after convert - " << size.template to_string<char, std::char_traits<char>,std::allocator<char> >();
------------output to console----------- size - <0110>, after convert - 1110
----------end of output--------------- Above is the output i get. Notice i am expecting after convert to
show 0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003. Hmmmm.....
If I understand what you are trying to do, I think all you would need
is this:
std::bitset<4> size(str.substr(pos,4));
std::cout << "next - <" << str.substr(pos,4)
<< ">, after convert - " << size;
Hi,
Thanks for the reply. I'm not able to do that because this occurs
within a while loop and I actually did a size.reset() after each loop.
Regards and thanks again.
Hi,
This is strange. I modified my codes to create new instances of
std::bitset<4> size for each loop of the while loop as you suggested
above and it is ok. Hmmm, I don't know why my reset() call is not
working still but at least this works, so I will stick with it for the
time being. Thanks again.
"DamonChong" <so********@excite.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com... Hi,
I am trying this in my code and getting strange answers. Would appreciate if someone can point out what it is i'm missing here. My thanks in advance.
-------------code snippets-------------- // where str is a std::string variable std::bitset<4> size; size.reset(); size |= std::atoi( (str.substr(pos,4)).c_str() );
std::cout << "next - <" << str.substr(pos,4) << ">, after convert - " << size.template to_string<char, std::char_traits<char>,std::allocator<char> >();
------------output to console----------- size - <0110>, after convert - 1110
----------end of output--------------- Above is the output i get. Notice i am expecting after convert to show 0110 but instead i get 1110!! BTW, i'm using VC++ Toolkit 2003. Hmmmm.....
Assuming str.substr(pos,4)) == "0110"
then
std::atoi( (str.substr(pos,4)).c_str() ) == 110
which in binary is 1101110
and the lower 4 bits are 1110
So what unexpected?
Jeff Flinn
Duh, i can't believe what a silly mistake this is! Thanks alot, I was
trying to convert binary (from a char array of ones and zeros) into
integer using the wrong method. Silly me ;P This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: SpOiLeR |
last post by:
Hi.
q1: Is std::bitset<N> part of standard or it's compiler extension?
q2: Is std::bitset::to_string() part of standard?
q3: My documentation say this about std::bitset::to_string():
...
|
by: Ben |
last post by:
Hi,
I need to write some data types into an array of unsigned chars.
These are basically "signals" within a message, so each signal will have
a start bit and a length. The signals will also...
|
by: Michal Wyrebski |
last post by:
Hello,
I was thinking how is the fastest and the best way of passing bitset
variable to some function.
Only one method came to my mind. It's about passing converted value:
#v+
void...
|
by: Artemis Fowl |
last post by:
when i used bitset<> to accept data from the user, much to my surprise,
i got the wrong answer. After a bit of fiddling, figured out that the
data being entered was being consumed in reverse. I...
|
by: tobias.sturn |
last post by:
How would be the best solution to create a dynamic bitset cause with
the stl bitset you have to give a number of how big the bitset will be
and i dont know the size at the beginning...
Thanks...
|
by: pedagani |
last post by:
Problem: read 'nBits' (where 0< nbits <= 32) from a buffer
'buffer' at a byte position 'pos' with a *bit* offset value of
'bitOffset'. Below is the code that does that (not tested for errors),...
|
by: felixnielsen |
last post by:
I would wery much like this to work:
@code start
#include <iostream>
#include <bitset>
const unsigned short size = 2; // 2^<unsigned int> //
union V {
char c;
std::bitset<size*size*size> b;...
|
by: Alan Ning |
last post by:
Hi all,
Say I have the following,
string str("Hello World\n")
I would like to extract 5 bits at a time from the string. I am doing
this to convert ascii string to another string type.
Is...
|
by: Guy.Tristram |
last post by:
Is there any good reason operator< is not defined for std::bitset? It
seems to me that:
1 - it would be useful.
2 - it is easy to implement inside the class template.
3 - it is impossible to...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |