Objective-C:如何从.NET的BitConverter类读取数据
在 .NET Web 应用程序中,BitConverter 类用于将 Int32 转换为字节数组,如下所示:
byte[] intBuffer = BitConverter.GetBytes(12345);
现在我尝试将其读回到 iPhone 应用程序中。我的 NSData 中的字节长度为 4,但我不确定如何继续,因为我不知道如何在调试器控制台中查看 nsdata 的内容。
NSData *intBuffer = [NSData dataWithBytes:[buffer bytes] length:4];
如何将这 4 个字节转换为 Objective-C int?
编辑:
这是我想出的一些东西,可能在正确的轨道上,也可能不在正确的轨道上。我不确定如何通过 NSData 中的索引获取单字节值。
- (int) extractSize:(NSData *)data {
NSMutableString *size = [NSMutableString stringWithCapacity:0];
for (int i = [data length] - 1; i <= 0; i--) {
//[size appendFormat:@"%x", data[i]];
}
return strtol([size UTF8String], nil, 16);
}
In a .NET web application the BitConverter class is being used to convert Int32's into a byte array like so:
byte[] intBuffer = BitConverter.GetBytes(12345);
Now I am trying to read this back into an iPhone app. I have the bytes in an NSData with a length of 4 but I'm not sure how to continue because I can't figure out how to view the contents of the nsdata in the debugger console.
NSData *intBuffer = [NSData dataWithBytes:[buffer bytes] length:4];
How can I convert these 4 bytes to an objective-c int?
Edit:
Here is something I came up with which may or may not be on the right track. I'm not sure how to get a single byte value by index in an NSData.
- (int) extractSize:(NSData *)data {
NSMutableString *size = [NSMutableString stringWithCapacity:0];
for (int i = [data length] - 1; i <= 0; i--) {
//[size appendFormat:@"%x", data[i]];
}
return strtol([size UTF8String], nil, 16);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)