473,385 Members | 1,400 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,385 software developers and data experts.

Converting to binary

60
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.
Jun 19 '07 #1
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
Jun 19 '07 #2
archonmagnus
113 100+
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
Jun 19 '07 #3
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. char bin_string[8];
  3.  
  4. itoa(num, bin_string, 2);
  5.  
  6. cout << bin_string << endl;    // outputs "101011"
  7.  
Jun 19 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Here's thing same thing written in C++:

Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. bitset<8> bin_string(num);    
  3. string str;
  4. str = bin_string.to_string();
  5. cout << str << endl;
  6.  
Jun 20 '07 #5
archonmagnus
113 100+
Here's thing same thing written in C++:

Expand|Select|Wrap|Line Numbers
  1. int num = 43;
  2. bitset<8> bin_string(num);    
  3. string str;
  4. str = bin_string.to_string();
  5. cout << str << endl;
  6.  
Well... I guess one learns something new every day. ;)
Jun 20 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Jun 21 '07 #7
archonmagnus
113 100+
The way that I was thinking to do it (which is hardly as elegant as weaknessforcat's above) is with the following code:

Expand|Select|Wrap|Line Numbers
  1. unsigned char bitMask = 0x80;
  2. unsigned short input = 43;  // outputs "00101011"
  3.  
  4. for (int i = 0; i < 8; i++)
  5. {
  6.     if (input & bitMask)
  7.         cout<<"1";
  8.     else
  9.         cout<<"0";
  10.  
  11.     bitMask =>> 1;
  12. }
  13.  
Jun 21 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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: ...
0
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...
0
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...
5
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...
2
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...
8
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...
8
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...
4
by: Beginner | last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice. Thanks.
1
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...
0
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...
1
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...
0
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...
0
isladogs
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...
0
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$) { } ...
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?
1
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...
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...

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.