please I can not solve it, some ideas? I see in DB only "System.Byte []" but no values from array.
Thank you.
Calling ToString() on a byte[] yields
System.Byte[]
But assuming that your byte[] contains a (UTF8 formatted) string, you could use
byte[] array= ...;
string myString = System.Text.Encoding.UTF8.GetString(array, 0, array.Length);
instead.
Related
I have a string which I need to convert to base64.
The default convert
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
I get a string which has '+' like "pURT+TFG=" and is converted to a space when sent as a get, so I can't compare to the original.
First, it sounds as a bad idea to send large sets of bytes in the query string. Short byte arrays should be fine. Make sure if this is what you need.
Second, you have to URL encode your base64 encoded string, by calling HttpUtility.UrlEncode or WebUtility.UrlEncode (prefer the latter):
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return WebUtility.UrlEncode(Convert.ToBase64String(cipherbytes));
Reading a Byte buffer:
while (...)
{
builder.Append(Encoding.ASCII.GetString(buffer, index, 1));
++index;
}
I'm getting the following result: "20202020202020202020202057363253304b4358", which looks like ASCII or HTML character codes. What is the best and faster way to obtain the real string out of that value in C#?
Although I think there is something wrong in your code while getting that string, anyway, you can use
byte[] buf = SoapHexBinary.Parse("20202020202020202020202057363253304b4358").Value;
var str = Encoding.ASCII.GetString(buf);
which would return W62S0KCX
PS: SoapHexBinary is in wellknown System.Runtime.Remoting.Metadata.W3cXsd2001 namespace :)
If you have the entire buffer available already, then simply try:
var myString = Encoding.Default.GetString(byteBuffer);
I have a uint value that I need to represent as a ByteArray and the convert in a string.
When I convert back the string to a byte array I found different values.
I'm using standard ASCII converter so I don't understand why I'm getting different values.
To be more clear this is what I'm doing:
byte[] bArray = BitConverter.GetBytes((uint)49694);
string test = System.Text.Encoding.ASCII.GetString(bArray);
byte[] result = Encoding.ASCII.GetBytes(test);
The bytearray result is different from the first one:
bArray ->
[0x00000000]: 0x1e
[0x00000001]: 0xc2
[0x00000002]: 0x00
[0x00000003]: 0x00
result ->
[0x00000000]: 0x1e
[0x00000001]: 0x3f
[0x00000002]: 0x00
[0x00000003]: 0x00
Notice that the byte 1 is different in the two arrays.
Thanks for your support.
Regards
string test = System.Text.Encoding.ASCII.GetString(bArray);
byte[] result = Encoding.ASCII.GetBytes(test);
Because raw data is not ASCII. Encoding.GetString is only meaningful if the data you are decoding is text data in that encoding. Anything else: you corrupt it. If you want to store a byte[] as a string, then base-n is necessary - typically base-64 because a: it is conveniently available (Convert.{To|From}Base64String), and b: you can fit it into ASCII, so you rarely hit code-page / encoding issues. For example:
byte[] bArray = BitConverter.GetBytes((uint)49694);
string test = Convert.ToBase64String(bArray); // "HsIAAA=="
byte[] result = Convert.FromBase64String(test);
Because c2 is not a valid ASCII char and it is replaced with '?'(3f)
Converting any byte array to string using SomeEncoding.GetString() is not a safe method as #activwerx suggested in comments. Instead use Convert.FromBase64String, Convert.ToBase64String
I am trying to save a byte array (byte[]) in c# application settings that is returned by Object List View.
Can anyone give me a solution on how to save byte array in c# application settings?
or some trick on how to convert byte[] to something like a string then store, then retrieve and again convert it to byte array and give it back to object list view.
One of the most common ways to make a string from an array of bytes is encoding them in Base-64:
string encoded = System.Convert.ToBase64String(toEncodeAsBytes);
Use
byte[] bytes = System.Convert.FromBase64String(encoded);
to get your bytes back.
The canonical way to do this is to convert the byte[] to a string via base64 and the other way round.
By Different way you can convert Byte array to string and string to byte array. Like this :
1)
string asciiString = ASCIIEncoding.ASCII.GetString(byteArray);
byte[] byte = ASCIIEncoding.ASCII.GetBytes(asciiString);
2)
string base64String = System.Convert.ToBase64String(byteArray);
byte[] byte = System.Convert.FromBase64String(base64String);
3)
string utf8String = System.Text.Encoding.UTF8.GetString(byteArray);
byte[] byte = System.Text.Encoding.UTF8.GetBytes(utf8String);
you can also use System.Text.Encoding.BigEndianUnicode, System.Text.Encoding.Unicode, and System.Text.Encoding.UTF32 for converting Byte Array to string and string to Byte Array.
Hope, It should help you.
I need very fast conversion from byte array to string.
Byte array is Unicode string.
From byte[] array to string
var mystring = Encoding.Unicode.GetString(myarray);
From string to byte[]
var myarray2 = Encoding.Unicode.GetBytes(mystring);
Try this
System.Text.UnicodeEncoding.Unicode.GetString
UTF8 (I think you mean "UTF8" instead of "Unicode"). Because, U'll get just Chinese Symbols. ;)
Maybe it helps to change...
var mystring = Encoding.Unicode.GetString(myarray);
...to...
var mystring = Encoding.UTF8.GetString(myarray);
:)