I'm in a class where the professor has just thrown us into the world of C with no help at all. So I would really appreciate any help anyone can give. I know Java and Visual Basic pretty well, so I'm not completely new and lost, I just don't understand what's going on here because I don't know the syntax.
The assingment he has given us this week is to put the chars of the input he will be testing our project with into an array and then taking them from that array and turning them into hex based on the ASCII table.
For example:
This is \t \t a test of the system.
Should turn into
54 68 69 73 20 69 73 09 09 61 20 74 65 73 74 20 6F 66 20 74 68 65 20 73 79 73 74 65 6D
Here is the code I have so far. This could all be horribly wrong because I have taken a lot of this from different forums and webpages all over the web. If anyone has any help, any changes, whatever, I would be glad to hear it!

#include <stdio.h>
#include <stdlib.h> 
#include <math.h>
#include <string.h>

int main(int argc, char **argv) 
{

    FILE *fp;
    int c;
    char temp;
    int num[1000];
    int j = 0;

    if (argc != 2) {
        fprintf(stderr, "%s input.txt\n", argv[0]);
        exit(1);
    }
    if (!(fp = fopen(argv[1], "r"))) {
        perror(argv[1]);
        exit(1);
    }
    while(!feof(fp)){
    temp = fgetc(fp);
    num[j] = temp - '0';
    fprintf(stdout, "%n", num[j]);

    j++;
    printf("%hhX", num[j]);
    }

    system("PAUSE");
    fclose(fp);
    return 0;
}

Edited 4 Years Ago by macdunk11

Yes. Everything should be working correctly. I think it's just my syntax that's wrong. I don't understand what exactly it's doing here.

All you need to do is read the character and output the same value in hex format. There are no calculations necessary.

This could all be horribly wrong because I have taken a lot of this from different forums and webpages all over the web. If anyone has any help, any changes, whatever, I would be glad to hear it!

The biggest problem with this technique is trying to mish-mash many different ideas together without understanding what they were doing.

I do the same as you as far as checking different sites for information. But they are only used as guides and information. Once I understand what they are explaining and how they help accomplish my task, I then write my own code because by then I understand the problem and the path to it's solution is clear.

Ok. My program specifically says that I need to put the characters into an array and when that array is full, it should call a function to print the hex and text representations for the characters. The fuction also needs to write the hex representation and, if the char is a special whitespace char (tab, new line, backspace, vertical tab, form feed, or any other non-printing char), it should call a function to print the escape sequence.
So how do I even create an array? Or store things in it? Or use it to call a function?

Ok. My program specifically says that I need to put the characters into an array and when that array is full, it should call a function to print the hex and text representations for the characters.

Fine. So read the file character by character. When the array is full, call the function. When the function returns, continue reading.

The fuction also needs to write the hex representation and, if the char is a special whitespace char (tab, new line, backspace, vertical tab, form feed, or any other non-printing char), it should call a function to print the escape sequence.

Makes sense. Test each character and output the hex if appropriate. If not, pass that character to this function.

So how do I even create an array? Or store things in it? Or use it to call a function?

Here's where you need to read your book. It's very basic.

what exactly hhx is doing ? i have listend and used hx only and x. but what is hhx in the above code ?

what exactly hhx is doing ? i have listend and used hx only and x. but what is hhx in the above code ?

hh is a new length specifier beginning with C99. It means that the object is the size of a signed or unsigned char being used as a small integer rather than a character.

x is hexadecimal. hx is short hexadecimal? am i right ? @deceptikon

h--> short ?
x is hexadecimal?

So how comes the point of char and unsigned in between this ?

x is hexadecimal. hx is short hexadecimal?

Yes.

So how comes the point of char and unsigned in between this ?

%hhx expects a single byte object and converts the value as hexadecimal.

are x and hx also want single byte object ? I don't think so.

Comments
Stop derailing other people's threads. If you have questions that will need 5 posts to get an answer, start your own thread with the question.

are x and hx also want single byte object ?

What? You just asked if %hx was a short and I said yes. How could %hx be both a short and a byte? %x expects a regular length integer (int or unsigned int).

yes yes! exactly ! that's was the confusion in my mind as i was thinking to relate hhx and hx. sorry for a tupid question, it's better to make your doubt clear (no matter what it's level) rather than being in confusion. ;) isn't it ? am i right or wrong in saying this thing ? @deceptikon

@waltP then why the hell you are downvoting every post ? you must say it in post, rather than downvoting like this. okay!

what is the problem if my problem is also get solved along with the cuurent thread. blown off now! limit it is. if it is also a rule on daniweb.com, then HUMAN being can reside on daniweb with these kinds of rules, this is fact!

Edited 4 Years Ago by nitin1

then why the hell you are downvoting every post ? you must say it in post, rather than downvoting like this. okay!

OK. Please stop derailing everyone else's threads with multiple questions of your own. They are asking for help on a specific question. When you start making multiple posts asking your own questions in their thread, it gets very confusing for everyone.

If you have a question not explicitly related to the current thread, start your own thread. Reference this thread if you want, but ask your question in your own thread, not theirs.

This thread is now much longer because you needed clarification on something that wasn't bothering the OP. You should have started your own thread for your discussion with deceptikon.

And the reason I didn't make a post was for this exact reason -- I didn't want to derail the thread any more than it has been, but you wouldn't let me.

Please be more considerate of other people's threads and their questions.

This article has been dead for over six months. Start a new discussion instead.