java - Converting buffer to unsigned int and using bitmask -


i have 4 bytes of data (stored unsigned int) use.

when 1 byte of data, has been solved using:

//java   buffer.get() & 0xff  //objective-c  uint32_t buffer; [data getbytes:&buffer range:nsmakerange(2, 1)]; uint32_t payloadlength = buffer & 0xffu; //data = d1021b53 //buffer = 897307; payloadlength: 27  

in java, for 4 bytes, has been done using:

//java  buffer.getint() & 0xffffffffl  //current not working objective-c code uint32_t buffer; [data getbytes:&buffer range:nsmakerange(2, 4)]; uint32_t payloadlength = cfswapint32bigtohost(buffer); //data: 0103940f b7626361 //buffer = 1656164244; payloadlength: 2484057954 (should = ~80) 

for objective-c / c, i'm having difficulty working 4 bytes , bitmasks.

anyone have this?

to convert 4 bytes integer can similar 1 byte code, must deal endian order - need know way around 4 bytes stored, low or high byte first.

so start 4-byte buffer:

uint32_t buffer; // or use int32_t if signed 

copy bytes:

[data getbytes:&buffer range:nsmakerange(0, 4)]; 

and convert order of bytes host order. if data big endian do:

uint32_t payloadlength = cfswapint32bigtohost(buffer); 

or if little endian:

uint32_t payloadlength = cfswapint32littletohost(buffer); 

and done.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -