CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    86

    4bytes int to 2bytes int conversion

    Hi...

    In C, I have int data type of 4 bytes...
    but I have to consider the lower 2 bytes only & have to put higher 2 bytes in another int

    Can any body tell me how to cast the int4 bytes to int2 bytes???

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: 4bytes int to 2bytes int conversion

    Hi!

    Code:
    int myint = ...;
    //low bytes:
    short low = myint & 0xFFFF;
    //high bytes:
    short high = myint >> 8;
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Dec 2008
    Posts
    86

    Re: 4bytes int to 2bytes int conversion

    Thank you for the help....

    I could solve it using char...... :

    Anyways thank you!!!

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: 4bytes int to 2bytes int conversion

    You could use the LOWORD and HIWORD macros too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured