将整数打包为字节(NSData)
我想将 MIDI 消息打包到 NSData 对象中。
int messageType = 3; // 0-15
int channel = 5; // 0-15
int data1 = 56; // 0-127
int data2 = 78; // 0-127
int packed = data2;
packed += data1 * 127;
packed += channel * 16129; // 127^2
packed += messageType * 258064; // 127^2 * 16
NSLog(@"packed %d", packed);
NSData *packedData = [NSData dataWithBytes:&packed length:sizeof(packed)];
int recovered;
[packedData getBytes:&recovered];
NSLog(@"recovered %d", recovered);
这工作得非常好,虽然我为自己感到自豪,但我知道到字节的转换没有正确完成:它应该是直接转换,不需要大量的加法和乘法。如何才能做到这一点?
编辑: 我现在知道我可以
char theBytes[] = {messageType, channel, data1, data2};
NSData *packedData = [NSData dataWithBytes:&theBytes length:sizeof(theBytes)];
在 Java 端
byte[] byteBuffer = new byte[4]; // Receive buffer
while (in.read(byteBuffer) != -1) {
System.out.println("data2=" + byteBuffer[3]);
}
执行此操作并且它会起作用,但我希望解决方案能够获得NSData 只有 3 个字节。
I want to pack a MIDI message into an NSData object.
int messageType = 3; // 0-15
int channel = 5; // 0-15
int data1 = 56; // 0-127
int data2 = 78; // 0-127
int packed = data2;
packed += data1 * 127;
packed += channel * 16129; // 127^2
packed += messageType * 258064; // 127^2 * 16
NSLog(@"packed %d", packed);
NSData *packedData = [NSData dataWithBytes:&packed length:sizeof(packed)];
int recovered;
[packedData getBytes:&recovered];
NSLog(@"recovered %d", recovered);
This works wonderfully and while I'm proud of myself, I know that the conversion to bytes is not done correctly: it should be a direct conversion without a lot of addition and multiplication. How can that be done?
Edit: I'm now aware that I can just do this
char theBytes[] = {messageType, channel, data1, data2};
NSData *packedData = [NSData dataWithBytes:&theBytes length:sizeof(theBytes)];
and on the Java side
byte[] byteBuffer = new byte[4]; // Receive buffer
while (in.read(byteBuffer) != -1) {
System.out.println("data2=" + byteBuffer[3]);
}
and it will work, but I'd like the solution to get an NSData with just 3 bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我会选择 NSString:
易于使用,且易于传输。拆包稍微复杂一些,但也不困难。
Personally, I would go for an NSString:
Easy to use, and easy to transfer. Unpacking is a tiny bit more complicated, but not difficult at all either.
这是我整理的一个 3 字节解决方案。
在线路的另一端,这也很容易获取,因为每个字节都是一个字节。我刚刚在iOS端和Java端都试过了,都没有问题。 字节顺序没有问题,因为每个整数都适合一个字节(或者在一种情况下两个整数适合一个字节)。
Here's a 3-byte solution that I put together.
and on the other side of the wire, this is just as easy to pick up, because each byte is a byte. I just finished trying this on the iOS side and the Java side, and there are no problems on either. There is no problem with endian-ness, because each integer fits into one single byte (or two in one byte, in one case).
你有几种选择。
因为看起来您想要 NSData 表示中的连续数据团...
您需要创建一个打包结构,并将数据作为预定义的字节顺序传递给 NSData 调用(因此两端都知道如何取消存档数据全局)。
you have several options.
since it looks like you want a contiguous glob of data in the NSData representation...
you'll want to create a packed struct, and pass the data to the NSData call as a predefined endianness (so both ends know how to unarchive the data glob).