Unicorn
Unicorn

Reputation: 313

How to input 8 byte hexadecimal number into char array?

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 the first 4 bits, meaning that 0 is truncated. and it prints 706050403020100.

So I was thinking of putting the number into a char array buffer(or some other 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.

Upvotes: 2

Views: 8137

Answers (1)

Kricket
Kricket

Reputation: 4179

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/

Upvotes: 2

Related Questions