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

converting a number into binary or hexadecimal or octal using arrays

Write a program in c++ to accept a number and convert this number into binary or hexa decimal or octal number according to the user choice using the concept of array.
Mar 1 '13 #1
6 3511
weaknessforcats
9,208 Expert Mod 8TB
For a moment, forget about C++ and let me ask you if you know how to convert a decimal number to binary?

If you don't, then no amount of C++ help is going to do any good.

So, take a decimal number, say 456, and convert it to binary.

Post the steps you took. Then we can start on the C++ implementation of your steps.
Mar 1 '13 #2
I don't know the steps :(
Mar 1 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
The binary for 456 is 111001000.

If you are using Windows you can use the Calculator set on Programmer view to do this so you can check your results.

OK. So the value of the binary columns is:

256 128 64 32 16 8 4 2 1

You need to determine whether there is a 1 or 0 in each column:

Calculate 456/256 to get 1
Now subtract 256 from 456 to get 200

Calculate 200/128 to get 1
Now subtract 128 from 200 to get 72

Caculate 72/64 to get 1
Now subtract 64 from 72 to get 8

Calculate 8/32 to get 0
Don't subtract 32 because 32 is bigger than 8

Calculate 8/16 to get 0
Don't subtract 16 because 16 is bigger than 8

Calculate 8/8 to get 1
Now subtract 8 from 8 to get 0

Calculate 0/4 to get 0
Don't subtract 4 because 4 is bigger than 0

Calculate 0/2 to get 0
Don't subtract 2 because 2 is bigger than 0

Calculate 0/1 to get 0
Don't subtract 1 because 1 is bigger than 0

You are done.

There are several ways to code this and at this point you should take a crack at doing the code. Do not worry if your code isn't perfect or efficient. Just make it work. Post again so I can see what you did.
Mar 2 '13 #4
thank you very much for your help, I really appreciate it.
Mar 2 '13 #5
donbock
2,426 Expert 2GB
I deleted a duplicate reply.
Mar 3 '13 #6
donbock
2,426 Expert 2GB
Please forgive the following precise, pedantic, and tiresome rant; but I think it will help to dispel some confusion. Your problem statement ought to be something more like this...

Write a program that does the following:
  1. Accept a user-entered string that contains a nonnegative decimal number.
  2. Validate the string (print an error if it contains anything other than the digits 0-9 or it has a value greater than can be held in whichever integral type you choose to use).
  3. Convert the string into a number.
  4. Print the value of the number as a string of binary digits.
  5. Print the value of the number as a string of octal digits.
  6. Print the value of the number as a string of hexadecimal digits.
You'll have to add the part about the user specifying the output format. Perhaps you are required to handle negative values.

You convert from string to number and then from number to string; but the integral number itself doesn't change once you've got it. Other posters have gotten quite confused -- thinking that they need to alter the way C stores integral values.

C++ has automagical ways to do these string conversions. It sounds like your assignment requires you to deliberately avoid doing it the easy way for the output strings. Are you allowed to take the easy way for converting the input string into the number?
Mar 3 '13 #7

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

Similar topics

3
by: Geoffrey | last post by:
I am working on a file conversion project that reads data from a one file format, reformats in and writes in out to another. The data is records of informations - names address, account...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
1
by: Neeraj Sharma | last post by:
Please Send Me The C program Which converts the decimal number to hexadecimal
0
by: antique | last post by:
iam supposed to CREATE A FUNCTION INT ADJUSTEDSUM (INT,INT) 1. find the sum of the array of size 100 2. find the number of occurrence of a MIN and MAX number if its twice or more only use 1...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
5
by: ria3 | last post by:
Hi, I have to write a program that will alow the user to specify a number (decimal, binary, or hexadecimal) and a base to convert to (decimal, binary, or hexadecimal) using subroutine methods and...
2
by: theblissfulwizard | last post by:
FYI - New to ASP I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do. I have a page that...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.