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

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

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?

Edited 3 Years Ago by pyTony: fixed formating

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.

desidude: try somthing like this:

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 :

string toHex(long num)
{
	char tmp[16];
	sprintf(tmp, "%x", num);
	return string(tmp);
}

Well for me it seems that he is trying to reinvent the wheel for a school projeckt, and not use standar function.

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'};
....

You could also write

char sign[] = "0123456789abcdef";

which might be a bit more manageable.

well i am lost guys
can anyone suggest me where and how should i start
i will really appreciate
thx

Well if you have to write the function that converts numbers into hexadecimal by yourself, try to understand the reuse the function posted by zyrus. Mine use standard library and do not fit for a school project.

Use modulus (this operator gets the remainder of a / b) 16 repeatedly to get your digits. Use a while loop while dividing the given number by 16 after modulus (use ints, you want truncation) until the given number becomes less than 16.

ie. You have a number, say 254.

254 % 16 = 14. This is your one's digit.
254 / 16 = 15.875 -> 15 after truncation.
15 % 15 = 15. This is your ten's digit.

As for digits, simply use an array of characters for simplicity. Add the characters to the front rather than the end, of course.

Edit: Whoops, not used to the reply tree here. My bad.

This article has been dead for over six months. Start a new discussion instead.