I have the following code to push two binary bytes to the clipboard:
DataFormats.Format binaryData = DataFormats.GetFormat("BinaryData");
DataObject data = new DataObject();
data.SetData(binaryData.Name, false, new Byte[] { 0, 1 });
Clipboard.SetDataObject(data, true);
In my own Windows program, if I pasted the clipboard with the above data in it and ran:
var obj = Clipboard.GetDataObject();
obj.GetData("BinaryData") always gives:
{byte[2]}
[0]: 0
[1]: 1
However, when pasted in two separate other applications, the BinaryData object received there is always 46 bytes as follows:
96 A7 9E FD 13 3B 70 43 A6 79 56 10 6B B2 88 FB 00 01 00 00
00 FF FF FF FF 01 00 00 00 00 00 00 00 0F 01 00 00 00 02 00
00 00 02 00 01 0B
What have I done wrong? Is an array of bytes the right class to use if I want to put some binary bytes to the clipboard?
Related
I'm reading a hex byte array from a file using an instance of the BinaryReader class, and I need to decode it from UTF-16. From what I know, I need to do this using System.Text.Encoding.Unicode.GetString(byte[] array).
This is what my code looks like:
public static string ReadHex(string path, Int32 startOffset, Int32 endOffset, bool decode, bool utf16)
{
if (!File.Exists(path))
{
throw new FileNotFoundException($"Could not find a file in path {path}");
}
string result = "";
List<byte> bytes = new List<byte>();
BinaryReader br = new BinaryReader(File.OpenRead(path));
for (int i = startOffset; i < endOffset; i++)
{
br.BaseStream.Position = i;
bytes.Add(br.ReadByte());
}
br.Close();
if (decode && utf16)
{
result = System.Text.Encoding.Unicode.GetString(bytes.ToArray());
}
else if (decode)
{
result = System.Text.Encoding.UTF8.GetString(bytes.ToArray());
}
else
{
foreach (byte b in bytes)
{
result += b.ToString("X2");
}
}
return result;
}
It reads the given hex offsets provided by the parameters and decodes them if wanted. The UTF-8 part works perfectly, giving me valid decoded strings. But my issue is with the UTF-16 part.
This is the data that is read from the file:
4D 00 6F 00 6A 00 61 00 6E 00 67 00 20 00 41 00 42
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00
In theory, this is what should be outputted:
Mojang AB
It kinda works, but with one oddity, this is what is outputted when writing the string that the method returns:
Mojang AB ?
These question marks appear on anything that's UTF-16 I put into the method. I don't understand why they appear, as the data that was passed into System.Text.Encoding.Unicode.GetString(byte[] array) does not contain anything past the string it's supposed to contain, and is made up of zeroes (00). Anyone have an idea on why this is happening?
I have this code which generate 80 bytes of word 'Administrator' to bellow output.
szOperatorName = BitConverter.ToString(data, 45, data.Length - 45); //szOperatorName is set to 'Administrator'
byte[] OperatorName = new byte[80];
Array.Copy(Encoding.ASCII.GetBytes(szOperatorName), OperatorName, System.Math.Min(80,szOperatorName.Length));
OUTPUT
41 64 6D 69 6E 69 73 74 72 61 74 6F 72 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
EXPECTED OUTPUT
41 00 64 00 6d 00 69 00 6e 00 69 00 73 00 74 00 72 00 61 00 74 00 6f 00 72 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I have highlighted some of main deference between two, any help on getting expected result will be much appreciated
To get expected output use Unicode encoding to get bytes
Encoding.Unicode.GetBytes(szOperatorName)
Test
byte[] bytes = System.Text.Encoding.Unicode.GetBytes("Administrator");
foreach (var b in bytes)
Console.WriteLine(b);
. . . .
Array.Copy(bytes, newArr, bytes.Length);
your current output is based on ASCII encoding (obviously)
I am trying to compare Finger print string & GPS data by converting byte array but result are not working for same finger record from different ends.
Here is my string/hex values from finger scan device & GPS accordingly.
Finger print encoded result as below.
AwFZFo4A//7//vAA4ACAAIAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSDRiePJYW3mEcxF5jKZueCSwPPiitjx4atw0+KT7LvimYFd9IIFe/diDbN1yihb91sAffQLoKnzsgbNw+JxT8OqmR/T+wThpGMkpWNSFU90mxyBcspZJSAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMBWRqRAP/+/wLgAMAAgACAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMZMXXlcZhF5SoAYeWKXbnh8qTx4RtA1eILsLfm8TWj8eFJWfPp0XX2sdWx9xI4cfa6wHvze2Cl8QQEyfdMBfvzCdbLpCQWMSQz6K8yGjEnY0pBT0MKZRlSQfa9I5rkySLB6V8yKg1FMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
GPS Tracker sending in HEX Code as below
03 01 5A 16 96 00 FF FE E0 02 C0 00 80 00 80 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 32 0B 16 9E 59 91 03 FE 6D 15 1A BE 55 98 85 7E 5D 9E 5B 7E 3A A5 0E 1E 16 2C 8C BE 25 B2 8B 1E 1E 0D 95 5F 75 9A C6 B7 24 23 8E BF 70 A3 87 7F 3B AE 49 FF 16 B7 8B DF 36 91 00 1C 41 14 56 FC 3D 91 16 9A 37 9D 14 F8 33 1F 92 19 30 16 2C 56 24 1C 12 36 2C 17 55 35 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
How to compare both with byte array??
Finger print code required to Convert FromBase64 string to Byte and GPS code required to Hex to byte..
I am getting different array size from Finger print it returning 512 byte array and from GPS it comes 256 byte array..
below is my code form C#
Create demo aspx page and check it.
I have added Demo project here.
you can download and check it..
http://maplayout.com/demo.zip
Thanks
Abhi
I have been resolved issue by converting HEX Code string to Byte[] and
USB reader value convert fromBase64 string then Byte[]
I have created below method to Compare both with byte[],
below fpengine is the thirdparty COM Object that will use for compare finger scanned value with finger point algorithm.
public bool IsMatchFound(string HexCodeString , string USBReaderString)
{
try
{
int scope = 0, intdisc=0;
byte[] gmat = HexEncoding.GetBytes(HexCodeString, out intdisc); //StringToByteArray(str1);
byte[] gref = Convert.FromBase64String(USBReaderString);
scope = fpengine.MatchTemplate(gmat, gref);
return (scope > 30) ? true : false;
}
catch (Exception ex)
{
return false;
}
}
Let me know if anybody want help same kind of application..
I have data in the form of from TextBox1
00 00 00 4E FF 53 4D 42 25 00 00 00 00 18 01 28 ...N.SMB%......(
00 00 00 00 00 00 00 00 00 00 00 00 00 08 12 8B ................
01 08 7E 31 11 00 00 05 00 00 00 00 00 00 00 02 ..~1............
and I tried to change it to be like this on the result TextBox2
...N.SMB%......(
................
..~1............
I've tried the function *. remove but only the first row are deleted but the second line and so on are not deleted. Can anybody help me?
i use this code
string test = rtText1.Text;
rtText2.Text = test.Remove(0, 48);
and then this result rtText2
...N.SMB%......(
00 00 00 00 00 00 00 00 00 00 00 00 00 08 12 8B ................
01 08 7E 31 11 00 00 05 00 00 00 00 00 00 00 02 ..~1............
how can I make a command "test.Remove (0, 48);" can be done on each line? or is there another function?
It's hard to say exactly what you need based on what you've posted, but a regex may work well here:
TextBox2.Text = System.Text.RegularExpressions.Regex.Replace(
TextBox1.Text,
#"^.{49}",
"",
RegexOptions.Multiline );
This will simply replace the first 49 characters of each line with an empty string.
It looks to me as though you're trying to remove the hexadecimal representation of whatever string is on the right.
This did the trick for me:
// Initial data
string Data = #"00 00 00 4E FF 53 4D 42 25 00 00 00 00 18 01 28 ...N.SMB%......(
00 00 00 00 00 00 00 00 00 00 00 00 00 08 12 8B ................
01 08 7E 31 11 00 00 05 00 00 00 00 00 00 00 02 ..~1............";
// An array containing each line of data
string[] lines = Data.Split('\n');
Data = "";
// Loop for each line, removing each "byte" + it's space, plus the trailing space
for (int i = 0; i < lines.Length; i++)
{
Data += lines[i].Remove(0, (3 * 16) + 1);
if (i != lines.Length - 1)
Data += "\r\n";
}
Console.Write(Data);
And here is the output:
...N.SMB%......(
................
..~1............
ideone link
I'm trying to parse a MIME-Message, using SharpMimeTools and some sample Mime Messages from Hunny Software. I managed to create a new Message from a file and save the Decoded Body to a file (it's a png-Image), but the created file is corrupted. Mostly the the example file and the one I've exracted look the same, but there are differences.
The files can be found here:
The original Message as a Text-File: m0013.txt
The origninal PNG-Picture: Blueball_original.png
The extracted, corrupted PNG-Picture: Blueball.png
An excerpt of the Hex-View of the files:
Original:
89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
00 00 00 1b 00 00 00 1b 08 03 00 00 00 ba 0a 04
67 00 00 03 00 50 4c 54 45 ff ff ff 00 00 08 00
00 10 00 00 18 00 00 00 00 08 29 00 10 42 00 10
4a 00 08 31 00 10 52 08 21 73 08 29 7b 08 29 84
08 21 6b 00 18 5a 00 08 39 08 21 63 10 39 9c 18
42 a5 18 42 ad 18 42 b5 10 39 a5 10 31 94 00 18
Extracted:
3f 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
00 00 00 1b 00 00 00 1b 08 03 00 00 00 3f 0a 04
67 00 00 03 00 50 4c 54 45 3f 3f 3f 00 00 08 00
00 10 00 00 18 00 00 00 00 08 29 00 10 42 00 10
4a 00 08 31 00 10 52 08 21 73 08 29 7b 08 29 3f
08 21 6b 00 18 5a 00 08 39 08 21 63 10 39 3f 18
42 3f 18 42 3f 18 42 3f 10 39 3f 10 31 3f 00 18
... and finally, this is the code I'm using:
public void MIMETest()
{
FileStream fileStream = new FileStream(#"D:\m0013.txt", FileMode.Open);
SharpMimeMessage m = new SharpMimeMessage(fileStream);
fileStream.Close();
parseMessage(m);
}
public void parseMessage(SharpMimeMessage message)
{
if (message.IsMultipart)
{
foreach (SharpMimeMessage subMessage in message)
{
parseMessage(subMessage);
}
}
else
{
System.IO.File.WriteAllText(#"D:\Extracts\" + message.Name,
message.BodyDecoded, message.Header.Encoding);
}
}
Do you have any suggestions how to solve this problem?
You are writing out binary files using WriteAllText. You cannot expect to write out a PNG using a text writer.
WriteAllText should only be used for text content-types. For other content-types you should use WriteAllBytes.
Also, in your code, you are writing the text using the original text encoding it was transmitted with. You probably want to just use UTF-8 regardless of what the original was.