CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: cast

  1. #1
    Join Date
    Mar 2008
    Posts
    15

    cast

    have a buffer of type integer (4 bytes) with 1000 elements. what is quickest method to typecast each element to short (2 bytes) before writing it a binary opened file?.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: cast

    A non-portable (doesn't work for big-endian machines) could be:
    Code:
    int32 arr[1000];
    FILE* pF;
    for( int i = 0; i < 1000; ++i ) fwrite( &arr[i], 2, 1, pF );
    An endian independent version requires a temporary buffer.

    Edit: I assume that you're sure that the elements only contains "16-bit values"?
    Last edited by S_M_A; July 20th, 2008 at 06:21 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jul 2008
    Location
    Ukraine, Kharkov
    Posts
    42

    Re: cast

    Why not just cast explicitly with "(short)" ?

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