Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

get specific byte from unsigned long long

I have an unsigned long long k (64 bits / 8 bytes).
If we number the bytes in k 0 - 7, with byte 0 being on the rightmost end of k, does anyone know of a way to get a specific byte out? For example, byte 3?
Thanks.
0
CalTree
Asked:
CalTree
  • 2
1 Solution
 
ExceterCommented:
Try this,

union t
{
    unsigned long long l;
    unsigned char s[8];
};

...

union t trans;
trans.l = 12000;
printf("%2x",trans.s[0]); // Print out the first byte of l in hexadecimal

Exceter
0
 
ExceterCommented:
>> 0 being on the rightmost end of k,

Ah,

printf("%2x",trans.s[7-0]); // First byte moving right to left
printf("%2x",trans.s[7-1]); // Second byte
printf("%2x",trans.s[7-2]); // Third byte

etc...

Exceter
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now