Hi Guys,
i want to convert a decimal value to binary. There is a function that does that in stdlib.h called itoa() but is it not standard hence not all compilers recognize it. Is there any other function (or series of funtions) that I can use. I can write mine from the scratch but I would like to know, first, if there is a function that does that.
7 3039
Hi,
as far as I am concern, itoa doesnt convert from decimal to binary. It converts from integer to string(ascii, thats why i(integer) to a(ascii)). For example, if you have an integer 1225, then itoa will convert it into "1225".
You will be better of writing your own code for decimal to binary conversion:)
Did you really meant to convert from decimal to binary or you wanted a standard function to convert from integer to string (similar to itoa may be)?
Thanks,
Sorower
Hi Guys,
i want to convert a decimal value to binary. There is a function that does that in stdlib.h called itoa() but is it not standard hence not all compilers recognize it. Is there any other function (or series of funtions) that I can use. I can write mine from the scratch but I would like to know, first, if there is a function that does that.
There aren't any standard functions to convert decimal to binary. It isn't that hard to write a conversion function. Strangely, it would be less time consuming to write the function from scratch than to search the web for accessing a (non-existant) built-in conversion. :p
Hi,
as far as I am concern, itoa doesnt convert from decimal to binary. It converts from integer to string(ascii, thats why i(integer) to a(ascii)). For example, if you have an integer 1225, then itoa will convert it into "1225".
You will be better of writing your own code for decimal to binary conversion:)
Did you really meant to convert from decimal to binary or you wanted a standard function to convert from integer to string (similar to itoa may be)?
Thanks,
Sorower
Actually, itoa()'s third arguement, is the base. So to convert from decimal to binary you would do this: -
int num = 43;
-
char bin_string[8];
-
-
itoa(num, bin_string, 2);
-
-
cout << bin_string << endl; // outputs "101011"
-
Here's thing same thing written in C++: -
int num = 43;
-
bitset<8> bin_string(num);
-
string str;
-
str = bin_string.to_string();
-
cout << str << endl;
-
Here's thing same thing written in C++: -
int num = 43;
-
bitset<8> bin_string(num);
-
string str;
-
str = bin_string.to_string();
-
cout << str << endl;
-
Well... I guess one learns something new every day. ;)
Well... I guess one learns something new every day. ;)
I'd love to take credit for this but it was AdrianH that put me onto it.
The way that I was thinking to do it (which is hardly as elegant as weaknessforcat's above) is with the following code: -
unsigned char bitMask = 0x80;
-
unsigned short input = 43; // outputs "00101011"
-
-
for (int i = 0; i < 8; i++)
-
{
-
if (input & bitMask)
-
cout<<"1";
-
else
-
cout<<"0";
-
-
bitMask =>> 1;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sverre Bakke |
last post by:
Hi
I am using the base_convert() function to convert numbers between binary,
hexadecimal, decimal, etc... It works great, but I have problems converting
numbers with .'s
Like this number:
...
|
by: Dan Stromberg |
last post by:
I've written up a page about how to convert native binary data to
another platform's native binary data, as I did some fortran data
conversions for a client.
The programs and documentation are...
|
by: Gurra |
last post by:
I am using XMLhttp and responsebody to receive info from a webpage
I dont use responsetext since i need the international chars هنِ
The information I recieve is binary is there any fast way to...
|
by: nickisme |
last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee
starter on c++... Just wondering if there's a quick way to convert data
into binary strings...
To explain, I'm trying to convert...
|
by: Mariusz Sakowski |
last post by:
I'm writing class which will be able to store large numbers (my
ambition is to make it able to operand on thousands of bits) and
perform various operations on it (similiar to those available with...
|
by: Rick |
last post by:
Hi,
Does C have some handy functions to convert chars, ints and floats to bit
arrays? I need to store that stuff binary so a few functions would be great.
Converting chars and ints isn't...
|
by: Marius Cabas |
last post by:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file...
|
by: Beginner |
last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice.
Thanks.
|
by: girl23 |
last post by:
Hi there
]i am new to C programming and totally lost.
iam trying to convert decimal to binary. here is what i did
please ignore the case h and m. I am trying to get case 'b' to work.
i do not...
|
by: Terry Reedy |
last post by:
A. Joseph wrote:
These are number representation systems that can be applied to or used
with integral, rational (numberator,denominator), and 'point' numbers.
Try Wikipedia or any search...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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: 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...
|
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...
| |