I'm pretty much new to using files, so I'm not sure if this is a banale question.
Here's the assignment: "Each line of the file dec.txt contains a whole number (>0). Write a program that creates a file called hex.txt. Each line of this file is to contain a string representing the hexadecimal equivalent of the whole number in the corresponding line of dec.txt."
make a character array (hexval) about 10 characters long
set whole array to spaces
set hexval[9] to '\0'
set hexptr to 8 -- this will point into hexval to load the hex chars.
Read a line into an integer (intval) -- that's base 10.
Loop until that integer is 0:
set hexval[hexptr] = remainder of intval/16
add 48 ('0') to hexval[hexptr] -- this converts the integer value to a digit character
if hexval[hexptr] > 57 ('9') add 7 -- value is now 'A' - 'F'
decrement hexptr -- to load the next hex digit
divide 16 from intval -- to remove the hex val we just loaded
End of loop
If you use that in dek_hex() you will have to change the return value from char to char* because as you can see from the above code snippet the hex value is an array of characters, not a single character value.
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X".
I'm not supposed to use any functions I'm not familiar with because I can't use them at my exam either. And my teacher never mentioned ssprintf, all I know out of that family is printf and fprintf.
If you use that in dek_hex() you will have to change the return value from char to char* because as you can see from the above code snippet the hex value is an array of characters, not a single character value.
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.