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

Dani AI

Generated

Useful, focused plan to finish this assignment and avoid the common pitfalls already seen in the thread.

Summary of needed parts: (1) normalize input to the 0..32767 range, (2) convert the normalized integer to hex, (3) write hex strings (no leading zeros) to an output file. and are right about using modulus/division; and show two valid ways to get hex. The missing pieces here are file I/O, a clear rule for values outside 0..32767, and some small loop/ordering fixes in 's attempt.

A safe normalization rule (explicit and easy to grade): treat negatives as 0 and wrap larger values by modulo 32768 so the result is in 0..32767. This is unambiguous and reproducible:

int normalize(int n) {
    if (n < 0) return 0;         // treat negatives as zero
    return n % 32768;           // wrap values > 32767 into 0..32767
}

A compact, robust conversion + file-processing outline using the standard library (avoids manual digit-prepending and automatically produces no leading zeros):

#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>

std::string to_hex(int n) {
    std::ostringstream oss;
    oss << std::uppercase << std::hex << n; // no leading zeros by default
    return oss.str();
}

int main() {
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");
    int v;
    while (in >> v) {
        int m = normalize(v);
        out << to_hex(m) << '\n';
    }
}

Troubleshooting notes tied to earlier posts: initialize variables, compute rem = value % 16 before dividing, use while (value != 0) (handle 0 as special case), and build digits LSB-first then reverse (or use the stream approach above). Confirm the program on edge cases: 0, 15, 16, 32767, 32768, 50000 and the negative example (-5). Also declare any missing variables (e.g., hexTotal) and avoid breaking on arbitrary remainder values (the rem == 1 bug in 's code).

Recommended Answers

All 13 Replies

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?

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.

dear friends
say somthing about nanotechnology

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.

There might be something quite close in the Code Snippets.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.