将32位值编码为ANSI,包括空(00)字节吗? [英] Encode 32 bit value as ANSI, including null (00) bytes?

查看:82
本文介绍了将32位值编码为ANSI,包括空(00)字节吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将32位二进制值放入剪贴板.用户单击一个按钮(复制二进制"),然后我希望该按钮将我代码中32位值的ANSI二进制编码值放入剪贴板.这是我正在尝试的代码:

I am trying place a 32bit binary value into the clipboard. The user hits a button ("Copy Binary") and I want that button to place the ANSI binary encoded value of the 32 bit value in my code into the clipboard. Here is the code I am trying:

//byte[] bytes contains my 32 bit value as a 4 byte array.

            Clipboard.SetData(System.Windows.DataFormats.Text, Encoding.Default.GetString(bytes));



如果字节"中的值不包含"00",即0x12345678,则此方法可以正常工作.

但是,如果值为0x12005678,它将仅在编码0x12后停止,并且我的剪贴板中只有一个字节.



This works correctly if the values in "bytes" doesn''t contain a "00", i.e. 0x12345678.

However, if the value is 0x12005678 it will stop after encoding just 0x12 and I will only have one byte in my clipboard.

How can I make sure that the entire byte array is encoded?

推荐答案

CF_TEXT是每个Windows桌面应用程序之间用于复制粘贴字符串的标准网关.将二进制数据作为CF_TEXT放置到剪贴板中是无稽之谈.无论如何,剪贴板专门处理这种类型,因此您没有碰到过运气.如果您将一些非标准的数据放入剪贴板,并且只能被您的程序或一组程序识别,那么您应该注册一种新的剪贴板格式,然后使用该格式复制/粘贴数据:为此,请在C#中执行以下操作:保存与获取自定义对象或从中获取自定义对象Windows剪贴板 [ ^ ]

解决该问题的另一种方法是将二进制值转换为数字的字符串表示形式,例如"365",并将其作为CF_TEXT存储在剪贴板上,然后在将其粘贴到程序中时必须将其转换回来.这样,您甚至可以使用记事本从文本文件中复制粘贴数字到程序中.
CF_TEXT is a standard gateway between every windows desktop apps to copy paste strings. Putting binary data as CF_TEXT to the clipboard is nonsense. Anyway, the clipboard handles this type specially so its a luck that you havent crashed. If you put some data to the clipboard that isn''t standard and is recognized only by your program or a set of your programs then you should register a new clipboard format and copy/paste your data with that format: Here is an article how to do that in C#: Saving and obtaining custom objects to/from Windows Clipboard[^]

Another solution to your problem can be converting your binary value to the string representation of the number like "365" and storing that as CF_TEXT on the clipboard, then you have to convert it back when you paste it in your program. This way you can copy paste a number to your program even from a text file with notepad.


您不能将二进制值转换为字符串,并希望它可以用作编码ASCII或其他任何形式的数据,将以不同的方式解释字节数据,从而在接收端进行更改.

在您的示例中就是这种情况,因为Windows在内部使用null(0x00)终止"ASCII"字符串.因此,您放置在剪贴板上的字符串只有一个字符长,不包括终止符.
You can''t convert a binary value to a string and expect it to work as the encoding for ASCII, or any other for that matter, will interpret the byte data differently, thereby altering it on the receiving end.

This is the case in your example as Windows, internally, uses a null (0x00) to terminate "ASCII" strings. So, the string you put on the clipboard is a single character long, not including the terminator.


这篇关于将32位值编码为ANSI,包括空(00)字节吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆