Reputation: 99
I've hit a roadblock in this code, and I'm going to be upfront and say that it is a homework assignment, but I've done most of it on my own so I feel okay about asking for some pointers for this last piece which is giving me trouble.
I had to write a program that reads in a list of numbers, prints it to stdout, uses bubble sort to sort it, prints it again, and then writes it to the specified output file. The sorting capabilities are working properly, and I've got it set up to read switches (-i or --input, signalling the input file; -o or --output, signalling the output file) from the command line, which also seems to be working. So that's all good!
We're supposed to set it up so that it can also read:
I'm confused about how to handle these last two requirements. I wish I could say that I was close to a solution, but I'm totally blanking and would really appreciate any help I can get. Should I create a new function that takes a pointer to the array of integers and go through some conversion process to make each element into a hexadecimal? Is there some quicker way to do this that's built into C that I've completely missed, or do I need to do some work with remainders and switch cases?
And will my bubbleSort function need to be adjusted to account for hexadecimal input? I think it will work for both decimal and hexadecimal input, but I'm not entirely sure...
Upvotes: 0
Views: 132
Reputation: 2067
You can use %x
format specifier to convert the integer value to hexadecimal value.
For example:
int data=30;
printf("%x\n", data);
This will output: 1e
Upvotes: 1
Reputation: 137
Check the documentation of fprintf and fscanf, you can put %X into the format string to read and write hexadecimal notation.
Upvotes: 0