Splitting unsigned long (newbie)

This is a discussion on Splitting unsigned long (newbie) within the C++ Programming forums, part of the General Programming Boards category; Hi there, sorry for my question,i'm newbie. I have a value like this in an unsigned long variable : B43D88CD ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Splitting unsigned long (newbie)

    Hi there,
    sorry for my question,i'm newbie.

    I have a value like this in an unsigned long variable :

    B43D88CD

    and want to convert it to :

    CD883DB4

    like a cross-exchange or mirror

    it's possible to do that or i need to convert into string and make a routine which replace every char ?

    Thanks.
    Dave

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If you have
    unsigned long x = 0xB43D88CD;

    Then
    y = ( x >> 24 ) & 0xFF;
    will get you the 0xB4

    Similar use of << and | will allow you to reassemble the bytes in whatever order you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    Quote Originally Posted by Salem
    If you have
    unsigned long x = 0xB43D88CD;

    Then
    y = ( x >> 24 ) & 0xFF;
    will get you the 0xB4

    Similar use of << and | will allow you to reassemble the bytes in whatever order you want.
    Thanks ! i've done that :

    y = INJ & 0xFF;
    z = y;
    y = (INJ >> 8) & 0xFF;
    z = (z << 8) | y;
    y = (INJ >> 16) & 0xFF;
    z = (z << 8) | y;
    y = (INJ >> 24) & 0xFF;
    z = (z << 8) | y;

    where INJ contain the original hex value, and it work !!!
    thanks again
    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Replies: 6
    Last Post: 07-19-2006, 05:00 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. Merge and Heap..which is really faster
    By silicon in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 04:06 PM
  5. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21