byte stream to int

Discussion in 'iOS Programming' started by OldGit, Feb 19, 2009.

  1. OldGit
    Expand Collapse
    macrumors newbie

    Joined:
    Feb 2, 2009
    #1
    In java there is a method "readInt()" which returns an int from byte array. What is the equivalent method to convert bytes to int (or float or other datatype)?
     
  2. jnic
    Expand Collapse
    macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Assuming 4-byte ints, something like:

    Code:
    int i = (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + b[3];
    Though be aware that sizeof(int) will likely differ depending on whether you're building for simulator or device.
     

Share This Page