Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
This will load the hex values from smallest to largest:
make a character array (<em>hexval</em>) about 10 characters long
set whole array to spaces
set <em>hexval[9]</em> to '\0'
set <em>hexptr</em> to 8 -- this will point into <em>hexval</em> to load the hex chars.
Read a line into an integer (<em>intval</em>) -- that's base 10.
Loop until that integer is 0:
set <em>hexval[hexptr]</em> = remainder of <em>intval</em>/16
add 48 ('0') to <em>hexval[hexptr]</em> -- this converts the integer value to a digit character
if <em>hexval[hexptr]</em> > 57 ('9') add 7 -- value is now 'A' - 'F'
decrement <em>hexptr</em> -- to load the next hex digit
divide 16 from <em>intval</em> -- to remove the hex val we just loaded
End of loop
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
you can use fscanf("%d", ...) to read them into an int memory, and fprintf("%X" ...) to write them back out as hex.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
FILE* fp ;
if( ( fp = fopen( "a.txt", "r" ) ) == NULL )
{
fputs( "file opening failed", stderr ) ;
exit( 1 ) ;
}
5. fscanf( ) normally returns the number of items scanned and in the case of error a NULL is returned.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>char dek_hex(int m)
You do realise that would return a single char. Is that what you want.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you could google for your question. Here is one way to do it. And another here .
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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 isextremely 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.2. what should my function look like?
Basic idea in my previous 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.
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? ;)
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>>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){
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Because EOF is actually a maro which is defined in stdio.h as #define <strong>EOF</strong> <em><integer constant expression < 0></em> 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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343