User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,707 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3431 | Replies: 17 | Solved
Reply
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

FILE + conversion from dec to hex = I need help

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 882
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #2  
Nov 11th, 2006
C or C++?
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

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

  #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
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 882
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

  #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  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

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

  #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 12:59 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 311
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

  #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 1:06 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,713
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 882
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

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

  #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:
char buf[20];
int i = 16;
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Oct 2006
Location: Montenegro
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 6:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC