tags:

views:

48

answers:

1

Hi

I want to generate sequence of hexadecimal numbers starting with 07060504003020100. Next number would be 0f0e0d0c0b0a0908 etc in that order.

When i use unsigned long long int and output the data first 4 bits ie 0 is truncated. and it prints 706050403020100.

So i was thinking of puting the number into a char array/buffer and then print the output so that later if i want to compare i can do character/byte wise compare.

Can anybody help me out? char[8]="0x0706050403020100" doesn't look right.

Any help would be highly appreciated.

+2  A: 

What are you using to print out your value? The syntax for printing 8 0-padded bytes to stdout would be something like

printf("%016X", value);

It breaks down like this:

%x    = lowercase hex
%X    = uppercase hex
%16X  = always 16 characters (adds spaces)
%016X = zero-pad so that the result is always 16 characters

For example, you could do the same thing in base 10:

printf("%05d", 3); // output: 00003

You can also play with decimals:

printf("%.05f", 0.5); // output: 0.50000

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Kelsey Rider
No i wanted to know how can store my initial value 0x0706050403020100.can i store it using character array or dynamically using malloc or using unsigned long long int ??
Thej
You can store your value anywhere where you've allocated at least 8 bytes of memory. This could be a long (depending on the hardware), a char[8], a long double (which might actually be 16 bytes), a struct that you make, etc.In your initial post you said your problem was that you don't see the first hex character when you output your number. This is normal - it's just like, when you output the value 3, you often don't want to see 0000000003. If you want leading zeroes, you have to tell printf to include them. But I can assure you that your machine has not "forgotten" about a nybble.
Kelsey Rider
I also might add that doingchar[8]="0x0706050403020100";will probably produce a segfault, because you're declaring 8 bytes and filling it with 18.
Kelsey Rider
@Rider:Thanks.But hw to tell printf to include leading zeroes.
Thej
So how should i declare using char(static or dynamically)??
Thej
To show leading zeroes, use printf like I showed you. :)I'll edit my original post to be more specific.
Kelsey Rider
I was just mentioning the char array as an example, but I doubt that a char array is the optimal solution. If you really want to use a char array, declare your char arr[8], then read your 8-byte value into the memory pointed to by arr. To print it, you'd then have to either loop over each char, printing each one as a zero-padded 2-digit hex value, or else cast arr as a (long *). Depending on the hardware, you may have issues like endianness to deal with. Like I said, probably not your ideal solution. :)
Kelsey Rider