FILE + conversion from dec to hex = I need help

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

Join Date: Aug 2005
Posts: 15,678
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: 1504
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
  #11
Nov 12th, 2006
you could google for your question. Here is one way to do it. And another here.
Last edited by Ancient Dragon; Nov 12th, 2006 at 3:24 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
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: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
1
  #12
Nov 12th, 2006
Originally Posted by Sin-da-cat View Post
So my questions would be:

1. is my general idea alright?
Yes. But you should pass in the hex array. C cannot return a character array. Also, format your code. Indentation is extremely important at all times. So is spacing between terms, like:
  1. while (fscanf(a, "%d", &broj) != EOF)
Much easier to read.

  1. char dek_hex(int m){ /* this is where my problems start. No matter
  2.   how I write the function, I keep getting all
  3.   kinds of error messages */
Unless you post something you've tried, we have nothing to help with.

Originally Posted by Sin-da-cat View Post
2. what should my function look like?
Basic idea in my previous post.

Originally Posted by Sin-da-cat View Post
3. can it be done without a function?
Yes it can. Considerations:
function: Modularizes the program, easier to reuse the function in another program later. Somewhat easier to debug.
non-function: Not passing the hex array is a minor benefit.


Originally Posted by Ancient Dragon
like I mentioned in my previous post, printf() family of functions, which includes ssprintf(), will convert int to hex using "%X" .
Wouldn't this nullify the purpose of understanding how decimal to hex works?
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
  #13
Nov 13th, 2006
OK, nevermind, I figured it out. I threw out the function and just did the whole conversion from dec to hex in the body of 'fscanf' with local variables. I realized using a function was a completely pointless complication.

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. void main(){
  5. FILE *c,*b;
  6. int a;
  7. c=fopen("C:\\C_fajlovi\\dec.txt","r");
  8. b=fopen("C:\\C_fajlovi\\hex.txt","w");
  9. if (c==NULL){
  10. puts("Error!");
  11. exit(1);
  12. }
  13. if (b==NULL){
  14. puts("Error!");
  15. exit(1);
  16. }
  17. while (fscanf(c,"%d",&a)!=EOF){
  18. char s[30],temp;
  19. int i=0;
  20. if (a==0)
  21. s[i++]='0';
  22. while (a>0){ /*turning a dec (int) to a hex (string) value*/
  23. if ((a%16)<=9)
  24. s[i++]=(a%16)+'0';
  25. else if ((a%16)==10)
  26. s[i++]='A';
  27. else if ((a%16)==11)
  28. s[i++]='B';
  29. else if ((a%16)==12)
  30. s[i++]='C';
  31. else if ((a%16)==13)
  32. s[i++]='D';
  33. else if ((a%16)==14)
  34. s[i++]='E';
  35. else
  36. s[i++]='F';
  37. a/=16;
  38. }
  39. s[i]='\0'; /*I was missing this part the last time, which is one of the reasons the whole program was acting crazy*/
  40. for (i=0;i<strlen(s)/2;i++){
  41. temp=s[i]; /*got a reversed string, turning it around*/
  42. s[i]=s[strlen(s)-i-1];
  43. s[strlen(s)-i-1]=temp;
  44. }
  45. fprintf(b,"%s\n",s);
  46. }
  47. }

If someone has any suggestions they're still welcome.
Last edited by Sin-da-cat; Nov 13th, 2006 at 4:20 pm.
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,678
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: 1504
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
  #14
Nov 13th, 2006
>>while (fscanf(c,"%d",&a)!=EOF){

fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error. What you want is this:
  1. while (fscanf(c,"%d",&a) > 0){
Last edited by Ancient Dragon; Nov 13th, 2006 at 4:28 pm.
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
  #15
Nov 14th, 2006
Originally Posted by Ancient Dragon View Post
>>while (fscanf(c,"%d",&a)!=EOF){

fscanf() does NOT return EOF one eod-of-file. EOF is returned only if there was a conversion error.
My thought: the while(fscanf... loop would go through the file line by line untill it gets to EOF, at which point it will stop. It works just fine in every program I write (including the one I posted), never had a problem with that.

What will fscanf return when it gets to the end? What you suggested works too, so I suppose it returns 0, but how come my version always works, then?
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,653
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
  #16
Nov 14th, 2006
Because EOF is actually a maro which is defined in stdio.h as

#define EOF <integer constant expression < 0>

The macro yields the return value used to signal the end of a stream or to report an error condition
In your case its the end of stream thing which is causing the break of loop.
In Mr. Dragons case it is the 0 which is returned by the fgets( ) which is causing the termination.
Last edited by ~s.o.s~; Nov 14th, 2006 at 12:03 pm.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
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: 1504
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
  #17
Nov 14th, 2006
Here is an example that will illustrate the difference between using EOF and 0. In this example, checking for EOF causes infinite loop because end-of-file is never reached due to conversion error. If you uncomment the while statement that check for 0 then the program will stop when conversion error occurs.

When you wrote your programs for school you probably wrote sample data files that did not contain errors. That will not always be the case in real-world.

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main(int ac, char** av)
  5. {
  6. int a;
  7. int counter = 0;
  8. FILE* fp = fopen("myfile.txt","r");
  9. if(fp != NULL)
  10. {
  11. while( fscanf(fp,"%d",&a) != EOF)
  12. // while( fscanf(fp,"%d",&a) > 0)
  13. {
  14. printf("%d\n",counter++);
  15. }
  16. fclose(fp);
  17.  
  18. }
  19. return 0;
  20. }

This is sample data file
  1. 1
  2. 2
  3. 40
  4. abc
  5. 5a
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
  #18
Nov 14th, 2006
Ah, I see what you mean. Had to put a 'getchar()' after 'fclose' though so the black screen would stay open more than a milisecond.

Thanks for the answers.
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


Views: 4571 | Replies: 17
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC