2

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.

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

7 Comments

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 ??
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.
I also might add that doing char[8]="0x0706050403020100"; will probably produce a segfault, because you're declaring 8 bytes and filling it with 18.
@Rider:Thanks.But hw to tell printf to include leading zeroes.
So how should i declare using char(static or dynamically)??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.