如何从NSPasteboard获取字节



Hex Fiend:中打开了一个二进制文件

十六进制恶魔快照

我突出显示了第一个288字节,并使用CMD+C进行复制

然后我试着从代码中检索:

NSPasteboard *pboard = NSPasteboard.generalPasteboard;
NSString *content = [pboard stringForType:NSPasteboardTypeString];
NSData *data1 = [content dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSData *data2 = [pboard dataForType:NSPasteboardTypeString];

data1data2看起来像:

{length = 435, bytes = 0x41c39a67 c28d3c18 c3847a1e c2bf5dc2 ... bac3884f 3274c3b8 }

我把CMD+V的内容粘贴到Sublime TextVS code中,它们都可以正确显示:

Sublime快照

我的问题是:如何从NSPasteboard取回确切的288字节

谢谢你们!


更新

NSPasteboard *pboard = NSPasteboard.generalPasteboard;
for (NSString *t in pboard.types) {
NSLog(@"pboard type: %@, data length: %ld", t, [pboard dataForType:t].length);
}
NSLog(@"----");
for (NSPasteboardItem *item in [pboard pasteboardItems]) {
for (NSString *type in [item types]) {
NSData *data = [item dataForType:type];
NSLog(@"item type: %@, data length: %ld", type, data.length);
}
}

结果:

2022-04-11 16:08:59.726507+0800 testapp[50006:2713553] pboard type: dyn.ah62d4rv4gu8yuvwusmy1q2pyqzbhw7dfkf3he2p3nbvg82pwqvnhw6df, data length: 341
2022-04-11 16:08:59.726649+0800 testapp[50006:2713553] pboard type: HFPrivateByteArrayPboardType, data length: 341
2022-04-11 16:08:59.733329+0800 testapp[50006:2713553] pboard type: public.utf8-plain-text, data length: 435
2022-04-11 16:08:59.733525+0800 testapp[50006:2713553] pboard type: NSStringPboardType, data length: 435
2022-04-11 16:08:59.733581+0800 testapp[50006:2713553] ----
2022-04-11 16:08:59.733722+0800 testapp[50006:2713553] item type: dyn.ah62d4rv4gu8yuvwusmy1q2pyqzbhw7dfkf3he2p3nbvg82pwqvnhw6df, data length: 341
2022-04-11 16:08:59.733806+0800 testapp[50006:2713553] item type: public.utf8-plain-text, data length: 435

我仍然无法获得预期的288字节


更新2

Clipboard Viewer的快照显示修改了不可见字符的字节。

HFPrivateByteArrayPoardType快照

不可见字符快照

是的,HexFiend是开源的,但代码对我来说很复杂:(我还没有弄清楚如何阅读HFPrivateByteArrayPboardTypeplist。

对不起,伙计们,我错了。

经过我的双重检查,无论是Sublime Text还是VS Code,都不能正确地返回原点288字节。它们实际上只能显示288个转换后的字符(435个字节(。我的坏!

似乎不可能通过通用字符串类型的剪贴板在不同的应用程序之间安全地传递二进制数据。(如果我错了,请纠正我:(

相关内容

  • 没有找到相关文章

最新更新