Thread: How to extract all 4 hex digits from an unsigned short int?

  1. #1
    Registered User Fernando Aires's Avatar
    Join Date
    Nov 2010
    Location
    Brazil - Florianópolis, SC
    Posts
    6

    Question How to extract all 4 hex digits from an unsigned short int?

    Hi, I'd like to know how it is possible to extract all 4 digits from an unsigned short int. I have a system where I use an unsigned short int and each "hex digit" represents something. For example:

    Code:
    0x1A2B
    I have to extract the hex digits 1, A, 2 and B from that value, but I don't know how to do that. I know how to extract nibbles from bytes, but an unsigned short int is usually two bytes in C, so I'm clueless.

    I appreciate your help!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are a number of ways to do this. It really just depends on how you want them split up.
    You can use bitwise operators: Cprogramming.com FAQ > Bit shifting and bitwise operations
    You could turn it into a string:
    Code:
    printf( "%x", 0x1a2b );
    Pick one, post your attempt and what problems you are having, and we'll push you in the right direction.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Fernando Aires's Avatar
    Join Date
    Nov 2010
    Location
    Brazil - Florianópolis, SC
    Posts
    6
    Thanks, I'll try some bitwise operations and see what I can do with them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unsigned Short to Char[X]
    By trooper in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2011, 03:31 AM
  2. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  3. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  4. Pure C# Unsigned Short*
    By fretwell in forum C Programming
    Replies: 5
    Last Post: 08-05-2002, 06:25 PM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM