hexadecimal.cpp

Reply

Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

hexadecimal.cpp

 
0
  #1
Jul 9th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 50
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: hexadecimal.cpp

 
0
  #2
Jul 9th, 2005
Hello,

Well, how do you convert decimal numbers to hex?

10 = A
11 = B
12 = C
13 = D
14 = E
15 = F

You should be able to do it out of division, or you can convert it to binary first and then go from binary to hex.

Work out how you would do it by hand, and then code it.

Christian
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,305
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: hexadecimal.cpp

 
0
  #3
Jul 9th, 2005
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.
"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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: desidude is an unknown quantity at this point 
Solved Threads: 0
desidude desidude is offline Offline
Newbie Poster

Re: hexadecimal.cpp

 
0
  #4
Jul 10th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,999
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: hexadecimal.cpp

 
0
  #5
Jul 10th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 2
Reputation: msathya is an unknown quantity at this point 
Solved Threads: 0
msathya msathya is offline Offline
Newbie Poster

Re: hexadecimal.cpp

 
0
  #6
Jul 10th, 2005
dear friends
say somthing about nanotechnology
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: hexadecimal.cpp

 
0
  #7
Jul 10th, 2005
desidude: try somthing like this:
  1. std::string Hex(int num)
  2. {
  3. std::string s1;
  4. char sign[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  5. int div,
  6. base = 16;
  7.  
  8. while (num >= 1)
  9. {
  10. div = num % base;
  11. num = num / base;
  12. s1 = sign[div] + s1;
  13. }
  14. return s1;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: hexadecimal.cpp

 
0
  #8
Jul 10th, 2005
A less funny solution :
  1. string toHex(long num)
  2. {
  3. char tmp[16];
  4. sprintf(tmp, "%x", num);
  5. return string(tmp);
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: hexadecimal.cpp

 
0
  #9
Jul 10th, 2005
Well for me it seems that he is trying to reinvent the wheel for a school projeckt, and not use standar function.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,999
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: hexadecimal.cpp

 
0
  #10
Jul 10th, 2005
Originally Posted by zyruz
  1. std::string Hex(int num)
  2. {
  3. std::string s1;
  4. char sign[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  5. ....
You could also write
  1. char sign[] = "0123456789abcdef";
which might be a bit more manageable.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC