user2309856
user2309856

Reputation: 99

C -- bubbleSort with conversions from decimal to hexadecimal

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:

  1. -x or --hexin to accept the input data in hexadecimal format from the input file. The data in the input are then assumed to be in hexadecimal format (without 0x)This is an optional argument. When missing, the data is assumed in decimals.
  2. -y or --hexout to output the results in hexadecimal format (without 0x) to the output file. This is an optional argument. When missing, the data needs to be in decimals.

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

Answers (2)

Amit Sharma
Amit Sharma

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

lnvd
lnvd

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

Related Questions