| | |
hexadecimal.cpp
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Solved Threads: 0
hi , Please take a moment to read this
i am trying to write hexadecimal.cpp program for my C++ programming class but i dont know how to do it.
the Project says as follows
You have to design and implement a C++ program that reads numbers from a file,converts them into hex numbers and write the hex numbers to output file. your program should have functions for subtask. It has to check if the number is between 0 and 32767. If it is greater than 32767, your Program should convert it to a number between 0 and 32767.
INPUT FILE
50000
-5
32767
138
15
also don'f forget to eliminate leading 0's in the display of the hex numbers.
i dont know how to write algorithm for hex numbers, rest i can take care of.
If sombody can help me with writing codes for hex numbers ,that will be of great help.
thx guys
i am trying to write hexadecimal.cpp program for my C++ programming class but i dont know how to do it.
the Project says as follows
You have to design and implement a C++ program that reads numbers from a file,converts them into hex numbers and write the hex numbers to output file. your program should have functions for subtask. It has to check if the number is between 0 and 32767. If it is greater than 32767, your Program should convert it to a number between 0 and 32767.
INPUT FILE
50000
-5
32767
138
15
also don'f forget to eliminate leading 0's in the display of the hex numbers.
i dont know how to write algorithm for hex numbers, rest i can take care of.
If sombody can help me with writing codes for hex numbers ,that will be of great help.
thx guys
We only give homework help to those who show effort
Searching the C and C++ Code Snippets might prove useful. So might doing a Google search on std::hex.
Searching the C and C++ Code Snippets might prove useful. So might doing a Google search on std::hex.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 15
Reputation:
Solved Threads: 0
guys i tried this'
#include <iostream>
using namespace std;
int input;
int rem;
char hex1;
int divisor;
int main()
{
cout<<"Enter the number :";
cin>>input ;
rem = 1;
divisor = input;
while (rem!= 0)
{
divisor = divisor / 16;
rem = divisor%16;
cout<<"Remainder is :"<<rem<<endl;
if (rem == 10)
hex1 = 'A';
if (rem == 11)
hex1 = 'B';
if (rem == 12)
hex1 = 'C';
if (rem == 13)
hex1 = 'D';
if (rem == 14)
hex1 = 'E';
if (rem == 15)
hex1 = 'F';
cout<<"divisor is :"<<divisor<<endl;
cout<<"HEX :"<<hex1<<endl;
hexTotal = hexTotal + hex1;
if (rem == 1)
break;
}
return 0;
}
any suggestion?
#include <iostream>
using namespace std;
int input;
int rem;
char hex1;
int divisor;
int main()
{
cout<<"Enter the number :";
cin>>input ;
rem = 1;
divisor = input;
while (rem!= 0)
{
divisor = divisor / 16;
rem = divisor%16;
cout<<"Remainder is :"<<rem<<endl;
if (rem == 10)
hex1 = 'A';
if (rem == 11)
hex1 = 'B';
if (rem == 12)
hex1 = 'C';
if (rem == 13)
hex1 = 'D';
if (rem == 14)
hex1 = 'E';
if (rem == 15)
hex1 = 'F';
cout<<"divisor is :"<<divisor<<endl;
cout<<"HEX :"<<hex1<<endl;
hexTotal = hexTotal + hex1;
if (rem == 1)
break;
}
return 0;
}
any suggestion?
Suggestion 1: Put your code in code tags.
Why are you breaking when the remainder is 1?
I think you want rem = divisor % 16 to appear before divisor = divisor / 16.
Your while condition should be: while (divisor != 0)
Also, you forgot the cases where your remainder is less than 10, in terms of setting hex1.
Overall, I'd say your algorithm is perplunked, and you need to do some more thinking.
Why are you breaking when the remainder is 1?
I think you want rem = divisor % 16 to appear before divisor = divisor / 16.
Your while condition should be: while (divisor != 0)
Also, you forgot the cases where your remainder is less than 10, in terms of setting hex1.
Overall, I'd say your algorithm is perplunked, and you need to do some more thinking.
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
desidude: try somthing like this:
C++ Syntax (Toggle Plain Text)
std::string Hex(int num) { std::string s1; char sign[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; int div, base = 16; while (num >= 1) { div = num % base; num = num / base; s1 = sign[div] + s1; } return s1; }
A less funny solution :
C++ Syntax (Toggle Plain Text)
string toHex(long num) { char tmp[16]; sprintf(tmp, "%x", num); return string(tmp); }
•
•
•
•
Originally Posted by zyruz
C++ Syntax (Toggle Plain Text)
std::string Hex(int num) { std::string s1; char sign[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; ....
C++ Syntax (Toggle Plain Text)
char sign[] = "0123456789abcdef";
![]() |
Other Threads in the C++ Forum
- Previous Thread: Again please someone check my code
- Next Thread: Messy array things.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






