FILE + conversion from dec to hex = I need help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

FILE + conversion from dec to hex = I need help

 
0
  #1
Nov 11th, 2006
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."

Can someone please walk me through this?
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1493
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: FILE + conversion from dec to hex = I need help

 
0
  #2
Nov 11th, 2006
C or C++?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: FILE + conversion from dec to hex = I need help

 
0
  #3
Nov 12th, 2006
This will load the hex values from smallest to largest:
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
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

Re: FILE + conversion from dec to hex = I need help

 
0
  #4
Nov 12th, 2006
C, not C++. Sorry, I should have said in the first place.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1493
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: FILE + conversion from dec to hex = I need help

 
-1
  #5
Nov 12th, 2006
you can use fscanf("%d", ...) to read them into an int memory, and fprintf("%X" ...) to write them back out as hex.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

Re: FILE + conversion from dec to hex = I need help

 
0
  #6
Nov 12th, 2006
Yes, it's fairly obvious that can be used. Iguess my question wasn't very to-the-point. Here's what I got so far, and what I need from you:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char dek_hex(int); /*a function I'll explain later*/
  5. main(){
  6. FILE *a,*b;
  7. int broj;
  8. a=fopen("C:\\C_fajlovi\\deci.txt","r"); /*created a file in the specified directory, it's all cool at this point*/
  9. b=fopen("C:\\C_fajlovi\\hex.txt","w"); /*create a new file to write to*/
  10. while(fscanf(a,"%d",&broj)!=EOF)
  11. fprintf(b,"%s",&dek_hex(broj)); /*dek_hex should be a function that turns a dec number into hexadecimal and returns it in the form of a string*/
  12. fcloseall();
  13. }
  14. char dek_hex(int m){ /*this is where my problems start. No matter how I write the function, I keep getting all kinds of error messages*/


So my questions would be:

1. is my general idea alright?
2. what should my function look like?
3. can it be done without a function?
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: FILE + conversion from dec to hex = I need help

 
0
  #7
Nov 12th, 2006
Okay here are a few pointers:

1. Use int main( ) not just main( ) and append a return 0 stmt at the end of main( ).

2. Dont hard code the file names in the function, its not a good practice. Just keep the file name in a constant array of characters.

3. I am not sure on this one, but, dont use \\ in your file paths, it is not cross platform complaint. Use forward slashes instead ( / ).

4. After opening the files do check whether the operation is successful, it is a good practice.

  1. FILE* fp ;
  2. if( ( fp = fopen( "a.txt", "r" ) ) == NULL )
  3. {
  4. fputs( "file opening failed", stderr ) ;
  5. exit( 1 ) ;
  6. }

5. fscanf( ) normally returns the number of items scanned and in the case of error a NULL is returned.
Last edited by ~s.o.s~; Nov 12th, 2006 at 1:59 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: FILE + conversion from dec to hex = I need help

 
0
  #8
Nov 12th, 2006
>char dek_hex(int m)

You do realise that would return a single char. Is that what you want.
Last edited by iamthwee; Nov 12th, 2006 at 2:06 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1493
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: FILE + conversion from dec to hex = I need help

 
0
  #9
Nov 12th, 2006
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" . Example:
  1. char buf[20];
  2. int i = 16;
  3. sprintf(buf,"%x",i);

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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

Re: FILE + conversion from dec to hex = I need help

 
0
  #10
Nov 12th, 2006
Originally Posted by Ancient Dragon View Post
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.

Originally Posted by Ancient Dragon View Post
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.
Yes, I'll do that. That was stupid of me.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC