ios - How to turn 4 bytes into a float in objective-c from NSData -
here example of turning 4 bytes 32bit integer in objective-c. function readint grabs 4 bytes read function , converts single 32 bit int. know how convert 4 bytes float? believe big endian. need readfloat function. can never grasp these bitwise operations.
edit: i forgot mention original data comes java's dataoutputstream class. writefloat function accordign java doc is
converts float argument int using floattointbits method in class float, , writes int value underlying output stream 4-byte quantity, high byte first.
this objective c trying extract data written java.
- (int32_t)read{ int8_t v; [data getbytes:&v range:nsmakerange(length,1)]; length++; return ((int32_t)v & 0x0ff); } - (int32_t)readint { int32_t ch1 = [self read]; int32_t ch2 = [self read]; int32_t ch3 = [self read]; int32_t ch4 = [self read]; if ((ch1 | ch2 | ch3 | ch4) < 0){ @throw [nsexception exceptionwithname:@"exception" reason:@"eofexception" userinfo:nil]; } return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); }
[data getbytes:&myfloat range:nsmakerange(locationwherefloatstarts, sizeof(float)]
ought trick.
Comments
Post a Comment