-1

I have an unsigned int array called hex_values which contains numbers. For example: The size of array is 5 and it contains this nubmers:

830C 830C 830C 830C 830C

What I need to do is to show them as big-endian values:

0C83 0C83 0C83 0C83 0C83

For coverting 830C to 0C83 I used this function :

unsigned int little_big(unsigned int little)
{
    return((little&0xff)<<24)+((little&0xff00)<<8)+((little&0xff0000)>>8)+      \
                                                             ((little>>24)&0xff);
}

The problem is , that if the first character of number is a zero, then printf will not show that zero ... Is there any way to fix it ?

THX for any help ...

edit :

Ok i have now another problem ... For example i have an array of size 5 which contains 5 values lets say 830C 830C 830C 830C 830C ... I have 2 functions (the first which is mentioned in my first post). The second function has this definition

void show_big(unsigned int * pt, unsigned int numbers)

{ <br>unsigned int hex_b, hex_l;

printf("\n");

for (unsigned int i = 0; i < numbers; i++)
{
    hex_l = *(pt + i);
    hex_b = little_big(hex_l);

    printf("%04X", (hex_b >> BITS));
}

}
I am calling this function with this parameters show_big(hex_values, numbers); // hex_values is the name of the array with 5 numbers and numbers is only a number which represent how many values has the array ... Problem is , if the array contains more values than 1 then show_big function wont show those numbers in big endian correctly (0C83 0C83 ...)... I have really no idea why ... If it is necessary i can past here full code ... THX for any help.

user1656755
  • 1
  • 1
  • 2
  • possible duplicate of http://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func – axis Oct 06 '12 at 09:54
  • 2
    @axis no, this question ironically has nothing to do with endianness. It's seems like it's actually about printing leading zeroes. – Martin Oct 06 '12 at 09:59
  • @Martin i was posting this before the edit, waiting for a more clear explanation from the author ... – axis Oct 06 '12 at 10:01
  • I'd suggest reproducing the problem with as simple a program as you can, posting the full source of that program to a new question, and accept Ivan's answer to your original question. Peoeple don't like answering questions with moving goalposts. – Martin Oct 07 '12 at 01:40

2 Answers2

1

Try printf("%05X", x), where x is the converted number. That instructs printf to print your number to at least 5 positions, and if takes less, to fill the leftmost ones with zeros.

Ivan Vergiliev
  • 3,771
  • 24
  • 23
0

your problem has to do with printf(), not with the way you are converting little endian number to big endian numbers.

for printf() to show leading zeroes, you should specify a length and precede the length with a 0:

printf("%04X", little_big(0x830C));

the number 4 tells printf() that it should output your number right aligned into a 4 characters long field, 0 tells printf() that it should pad the field with leading zeroes, and X is the format specifier for an hexadecimal output with uppercase letters.

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73