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,962 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,631 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: 3432 | Replies: 17 | Solved
Reply
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,721
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: 884
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

  #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 2:24 pm.
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

  #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:
while (fscanf(a, "%d", &broj) != EOF)
Much easier to read.

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 */
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?
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

  #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 3: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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,721
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: 884
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

  #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:
while (fscanf(c,"%d",&a) > 0){
Last edited by Ancient Dragon : Nov 13th, 2006 at 3:28 pm.
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

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

  #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 11:03 am.
"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
Location: near St Louis, Missouri, USA
Posts: 10,721
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: 884
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

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

#include <stdlib.h>
#include <stdio.h>
 
int main(int ac, char** av) 
{
	int a;
	int counter = 0;
	FILE* fp = fopen("myfile.txt","r");
	if(fp != NULL)
	{
		while( fscanf(fp,"%d",&a) != EOF)
//		while( fscanf(fp,"%d",&a) > 0)
		{
			printf("%d\n",counter++);
		}
		fclose(fp);

	}
	return 0;
}

This is sample data file
1
2
40
abc
5a
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

  #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  
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 7:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC