I have following string of bytes
17 80 41 00 01 00 01 00 08 00 44 61 72 65 46 61 74 65 01 00 00 00 01 00 03 00 01 00 09 00 43 68 61 6E 6E 65 6C 2D 31 00 00 02 00 09 00 43 68 61 6E 6E 65 6C 2D 32 65 00 03 00 09 00 43 68 61 6E 6E 65 6C 2D 33 65 00
What is the best way to take it as input from user and make it into byte array?
Try:
string text = ...
byte[] bytes = text.Split()
.Select(t => byte.Parse(t, NumberStyles.AllowHexSpecifier))
.ToArray();
If you want to only split on the space-character (rather than any whitespace) use Split (' ').
If the user is inputting it into the command line just like that, do this:
string input = GetInput(); // this is where you get the input
string[] numbs = input.Split(' ');
byte[] array = new byte[numbs.Length];
int i = 0;
foreach (var numb in numbs)
{
array[i++] = byte.Parse(numb, NumberStyles.HexNumber);
}
You can use the Parse method in System.Byte to parse the individual hax pairs:
// Get the string from the user
string s=Console.ReadLine();
// Convert to a byte array
byte[] sBytes=s.Split(new char[] {' '})
.Select(hexChar => byte.Parse(hexChar,NumberStyles.HexNumber))
.ToArray();
// *** Test code follows ***
// Display the bytes (optional), to verify that the conversion worked
StringBuilder hexString=new StringBuilder(sBytes.Length*3);
foreach (byte b in sBytes)
{
// Separate hex pairs with a space
if (hexString.Length>0)
hexString.Append(' ');
// Append next hex pair (i.e., formatted byte)
hexString.AppendFormat("{0:x2}",b);
}
Console.WriteLine(hexString);
Related
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?
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..
Question: Is it possible (and how?) to append a byte array to an existing file without loading the entire file in memory.
To be more specific:
the goal is to do transform this:
00000000 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
00000010 00 00 10 00 00 00 08 00 08 02 00 00 00 29 F0 61
00000020 E9 00 00 00 09 70 48 59 73 00 00 17 12 00 00 17
00000030 12 01 67 9F D2 52 00 00 20 00 49 44 41 54 78 DA
00000040 DC BD DB 76 24 39 AE 2C 08 F0 E2 1E AA EA F9 FF
00000050 FF 9C 5E A7 53 EE BC 60 1E 8C 80 C3 AF 0A 29 B3
00000060 76 EF 33 7A A8 A5 52 46 F8 85 04 41 10 30 33 30
00000070 33 D3 37 7F 44 E4 07 DF FA 5F F5 C3 CC 22 F2 F0
00000080 AF 97 6F 8A AF E0 BB 0F 57 B0 7F 12 22 3E FD FD
into this:
--added bytes
00000000 74 65 73 74 74 65 73 74 74 65 73 74 74 65 73 74
--added bytes
00000010 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
00000020 00 00 10 00 00 00 08 00 08 02 00 00 00 29 F0 61
00000030 E9 00 00 00 09 70 48 59 73 00 00 17 12 00 00 17
00000040 12 01 67 9F D2 52 00 00 20 00 49 44 41 54 78 DA
00000050 DC BD DB 76 24 39 AE 2C 08 F0 E2 1E AA EA F9 FF
00000060 FF 9C 5E A7 53 EE BC 60 1E 8C 80 C3 AF 0A 29 B3
00000070 76 EF 33 7A A8 A5 52 46 F8 85 04 41 10 30 33 30
00000080 33 D3 37 7F 44 E4 07 DF FA 5F F5 C3 CC 22 F2 F0
I've been trying to figure this out the whole morning, but I can't seem to find a working solution.
I know it is possible to append a file at the end without loading the file into memory, but I need to append the beginning.
Thanks in advance
Simply put, that's not possible. That's not how files work. Depending on how you do it, you may not have to load the whole file into memory at the same time, but you will need to load every single byte of the file and write it after the bytes you wrote to the front.
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.