开发者

How to convert int to big-endian 32-bit number in Objective C?

开发者 https://www.devze.com 2023-02-28 12:28 出处:网络
I\'m trying to implement WebSocket handshake (76) in ObjC and can\'t figure out (as I\'m new to C and ObjC) how can I get the following:

I'm trying to implement WebSocket handshake (76) in ObjC and can't figure out (as I'm new to C and ObjC) how can I get the following:

  1. List item
  2. I've got two number

    155712099
    173347027开发者_如何学运维
    
  3. I need to convert them to 32 bit big-endian numbers

  4. concatenate them into a string
  5. and express the resulting string in ASCII

Help is highly appreciated.


Since you're using the iPhone SDK, you have access to Core Foundation. Use:

uint32_t myInt32Value = ...;
uint32_t myInt32AsABigEndianNumber = CFSwapInt32HostToBig(myInt32Value);


Objective-C is based on C and usually has the C standard library available, so you can use the standard htonl function. You can then do the string manipulation using the normal string functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消