开发者

How to input 8 byte hexadecimal number into char array?

开发者 https://www.devze.com 2023-01-12 11:22 出处:网络
I want to generate sequence of hexadecimal numbers starting with 07060504003020100. Next number would be 0f0e0d0c0b0a0908 etc in that order.

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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消