I want to compare the first few bytes in byte[] with a string. How can i do this?
You must know the encoding of the byte array to properly compare them.
For example, if you know your byte array is made of UTF-8 bytes, then you can create a string from the byte array:
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
string s = enc.GetString(originalBytes);
Now you can compare string s to your other string.
Conversely, if you want to compare just the first few bytes, you can convert the string into a UTF8 byte array:
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] b = enc.GetBytes(originalString);
Now you can compare byte array b to your other byte array.
There are several other encoding objects for ASCII, Unicode, etc. See the MSDN page here.
use
byte [] fromString = Encoding.Default.GetBytes("helloworld");
Related
I want to convert a String to ByteArray in C# for Decrypt some data.
When I get de String from the ByteArray created, it shows question marks (?).
Example code:
byte[] strTemp = Encoding.ASCII.GetBytes(strData);
MessageBox.Show(strData);
MessageBox.Show(Encoding.ASCII.GetString(strTemp));
The string is "Ê<,,l"x¡" (With the double quotation mark) and the result to convert again to string is: ???l?x?
I hope this helps you:
To Get byte array from a string
private byte[] StringToByteArray(string str)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(str);
}
To get a string back from byte array:
private string ByteArrayToString(byte[] arr)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetString(arr);
}
For specific of that input, this BigEndianUnicode encoding seems works fine
byte[] strTemp = Encoding.BigEndianUnicode.GetBytes(strData);
MessageBox.Show(strData);
MessageBox.Show(Encoding.BigEndianUnicode.GetString(strTemp));
`
You are getting a byte array for the ASCII representation of your string, but your string is Unicode.
C# uses Unicode to encode strings, Unicode being able to represent far more symbols as ASCII.
In your example, every symbol which has no ASCII representation is replaced by '?', this is why only 'l' and 'x' appear in the output.
The proper way to do it is to use a Unicode encoding instead:
byte[] strTemp = Encoding.UTF8.GetBytes(strData);
MessageBox.Show(strData);
MessageBox.Show(Encoding.UTF8.GetString(strTemp));
Basically, any Unicode encoding can be used: UTF8, UTF32, Unicode, BigEndianUnicode (https://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings).
We know that the constructor of the class UTF8Encoding can receive an optional parameter: a bool specifying if the encoder should provide a byte order mark (BOM) or not.
However, when encoding the same text using both approaches, the output is the same:
string text = "Hello, world!";
byte[] withBom= new UTF8Encoding(true).GetBytes(text);
byte[] withoutBom = new UTF8Encoding(false).GetBytes(text);
Both withBom and withoutBom have the same content, one doesn't even have one byte more than the other one.
Why does this happen? Why is the byte order mark not being added to withBom?
BOM parameter in the constructor does no affect the result of GetBytes, it affects the result of GetPreamble. Users are expected to append it manually.
byte[] bom = new UTF8Encoding(true).GetPreamble(); // 3 bytes
byte[] noBom = new UTF8Encoding(false).GetPreamble(); // 0 bytes
The BOM is returned via the UTF8Encoding.GetPreamble method:
UTF8Encoding enc = new UTF8Encoding(true);
byte[] withBom = enc.GetPreamble().Concat(enc.GetBytes(text)).ToArray();
In my application i write the code like this
byte[] byt = new byte[Convert.ToSbyte(textbox1.Text)];
it is giving the error that input string was not in a correct format.
This is a wild guess, but are you trying to convert the contents of the text box into a byte array? If so, you can do it like this:
byte[] byt = Encoding.UTF8.GetBytes(textbox1.Text);
The text in textbox1 is not a valid numeral for a signed byte.
Does it have spaces? Letters? ...?
What are you trying to do? The new byte[num] creates an array of 'num' bytes, where 'num' is usually an integer. All bytes in the array are then 0.
It doesn't create a filled array, as I suspect you may be trying to do.
What are the contents of that textbox1.Text that gave the error?
what you want in fact is this
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
I have a string which contains binary data (non-text data).
How do I convert this to a raw byte array?
A string in C# - by definition - does not contain binary data. It consists of a sequence of Unicode characters.
If your string contains only Unicode characters in the ASCII (7-bit) character set, you can use Encoding.ASCII to convert the string to bytes:
byte[] result = Encoding.ASCII.GetBytes(input);
If you have a string that contains Unicode characters in the range u0000-u00ff and want to interpret these as bytes, you can cast the characters to bytes:
byte[] result = new byte[input.Length];
for (int i = 0; i < input.Length; i++)
{
result[i] = (byte)input[i];
}
It is a very bad idea to store binary data in a string. However, if you absolutely must do so, you can convert a binary string to a byte array using the 1252 code page. Do not use code page 0 or you will lose some values when in foreign languages. It just so happens that code page 1252 properly converts all byte values from 0 to 255 to Unicode and back again.
There have been some poorly written VB6 programs that use binary strings. Unfortunately some are so many lines of code that it is almost impossible to convert it all to byte() arrays in one go.
You've been warned. Use at your own peril:
Dim bData() As Byte
Dim sData As String
'convert string binary to byte array
bData = System.Text.Encoding.GetEncoding(1252).GetBytes(sData)
'convert byte array to string binary
sData = System.Text.Encoding.GetEncoding(1252).GetString(bData)
Here is one way:
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] theBytes = encoding.GetBytes("Some String");
Note, there are other encoding formats you may want to use.
Using the function from: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx
public static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
As you can see it returns a byte array, I want to convert the byte array to a string.
How can I convert it from a byte array to string and visa versa?
If you don't care how it's stored, an easy way is to use:
Convert byte array into string: Convert.ToBase64String(YourByteArray) and
Convert string into byte array: Convert.FromBase64String(YourString).
This will give a concise, printable ASCII representation of the byte array.
This can help you a lot, is about to converting into Hex format but can be very usefull
How do you convert Byte Array to Hexadecimal String, and vice versa?
System.Text.Encoding.ASCII.GetString(bytes);
While using Rijndael Encryption i faced this problem, it returns encrypted byte[] (array),
Convert byte[] to string;
myStringVariable= Convert.ToBase64String(myEncryptedByteArray);
Convert string to byte[];
byte[] bytes = Convert.FromBase64String(myStringVariable);
For more about Rijndael
Cheers !!!