Thread: 64bit number to 16bit number

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Question 64bit number to 16bit number

    Hi Everyone

    I have a 64bit mac address that I want to convert to a 16bit short address (by taking the last 4 numbers from the end of the mac:

    MAC: 0xAAAAAAAAAAAAABCD

    SHORT: ABCD

    I have tried the obvious with:

    Code:
    int main()
    {
        uint64_t macAddress = 0xABCDEF0123456789;
        uint16_t shortAddress = 0x0000;
        shortAddress = macAddress;
        printf("Short address is: %02X \n", shortAddress);
        return 0;
    }
    Which works, but I get a compiler warning:

    Integer constant is to large for long type
    Can anyone suggest another way of doing it that will result in no compiler warnings?

    Many thanks

    David

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems to me that your uint64_t is not defined correctly, or you need to postfix the number with LL as "long long".

    You probably also should have a cast on the shortAddress = macAddress; line, so that the compiler realizes that you are willingly loosing precision.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    ah! that did it! - Thanks

    Code:
    int main()
    {
        uint64_t macAddress = 0xABCDEF0123456789LL;
        uint16_t shortAddress = 0x0000;
        shortAddress = (uint16_t)macAddress;
        printf("SHort address is: %02X \n", shortAddress);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM