can not convert byte array to string and vice versa - c#

I am trying to convert byte array to string but bytes are not being converted to string correctly.
byte[] testByte = new byte[]
{
2, 200
};
string string1 = Encoding.ASCII.GetString(testByte);
byte[] byte1 = Encoding.ASCII.GetBytes(string1);
string1 is giving value as \u0002? and byte1 is not getting converted back to 2 and 200. I tried with UTF8 but that is also giving same problem.
I have been given 256 array of chars and integer values. I need to write these values on media as string and read back as bytes. I need conversion to write and read byte data. I am facing problems when integer value comes more then 127.
What should I do so I get original byte values from string?

You appear to be using an encoding backwards. A text Encoding(such as ASCII) is for converting arbitrary text data into encoded (meaning: specially formatted) binary data.
(a caveat should be included here that not all encodings support all text characters; ASCII only supports code-points 0-128, for example, but that isn't the main problem with the code shown)
You appear to want to treat arbitrary binary data as a string - which is the exact opposite. Arbitrary binary data, and encoded text data. Not a problem: just use base-N for some N. Hex (base-16) would work, but base-64 will be more space efficient:
string encoded = Convert.ToBase64String(testByte);
byte[] decoded = Convert.FromBase64String(encoded);

Related

Textbox data to Byte then sent to serialport & vice versa

I am very new to C# and using VS, but need a little help.
I have a textbox where a user can put in a value, for example "658". I want to convert this into bytes first (max 3 bytes) before sending it to the serialport. So the first byte sent is 0x02 and the second byte sent is0x92.
The second thing I am having issues with is the same but in reverse. I receive data in bytes, for example "0x0B, 0xC7, 0x14" and then I need to convert them into a decimal value and display them in a different Textbox.
I have tried a number of conversions that did not seem to work (parse, Tobyte and even using binary converter) so I am in need of help.
Thanks
This should get you started:
Convert From Numeric to Bytes:
var textInput = "658";
// validate...
var numericInput = Convert.ToInt32(textInput);
var convertedToBytes = BitConverter.GetBytes(numericInput);
// if your system is little endian (see below), reverse array output.
Convert From Bytes to Numeric:
// fourth octet is required to convert to an int32, which requires 4 bytes.
var bytesInput = new byte[] { 0x0, 0x0B, 0xC7, 0x14 };
// if your system is little endian (see below), reverse array.
var convertedFromBytes = BitConverter.ToInt32(bytesInput, 0);
Note, you want to pay attention to endian-ness. See this: https://msdn.microsoft.com/en-us/library/bb384066.aspx
You can use Encoding.GetBytes and Encoding.GetString to convert string to byte[] and back.
https://msdn.microsoft.com/ru-ru/library/ds4kkd55(v=vs.110).aspx
https://msdn.microsoft.com/ru-ru/library/744y86tc(v=vs.110).aspx
That should no be a problem as both the sending and the receiving serial port will accept/return a byte array. So the question comes down to how you create a by array from a string.
byte[] bytes = Encoding.ASCII.GetBytes(textBox1.Text);
The way back is:
string s = Encoding.ASCII.GetString(bytes);

UTF8 byte[] to string conversion

I have UTF8 byte[] of infinite size (i.e. of very large size). I want to truncate it to 1024 bytes only and then convert it to string.
Encoding.UTF8.GetString(byte[], int, int) does that for me. It first shortens 1024 bytes and then gives me its converted string.
But in this conversion, if last character is of UTF8 character set, which is made of 2 bytes and whose first byte falls in range and another byte is out of range then it displays ? for that character in converted string.
Is there any way so that this ? does not come in converted string?
That's what the Decoder class is for. It allows you to stream byte data into char data, while maintaining enough state to handle partial code-points correctly:
Encoding.UTF8.GetDecoder().GetChars(buffer, 0, 1024, charBuffer, 0)
Of course, when the code-point is split in the middle, the Decoder is left with a "partial char" in its state, but that doesn't concern you in your case (and is desirable in all the other use cases :)).

Change string to Byte Array without conversion

I have a JSON formatted object, and the byte array is coming through as a string. I need to change that string to a byte array, but without converting the char's.
static byte[] GetBytes(string str)
{
return str.Select(Convert.ToByte).ToArray();
}
The above code half solves the issue, unfortunately it's still converting each char to it's respective byte.
For completness, here is my string
"PCFET0NUWVBFIGh0bWwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMDEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvVFIvaHRtbDQvc3RyaWN0LmR0ZCI+PGh0bWw+PGhlYWQ+PE1FVEEgaHR0cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTE2Ij48dGl0bGU+Q2l2aWwgUHJvY2VkdXJlIGluIE1hZ2lzdHJhdGVzJyBDb3VydHM8L3RpdGxlPjxsaW5rIHR5cGU9InRleHQvY3N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hdGgubWF4KGRvY3VtZW50LmJvZHkuc2Nyb2xsSGVpZ2h0LCBkb2N1bWVudC5kb2N1bWVudEVsZW1lbnQuc2Nyb2xsSGVpZ2h0KSwgTWF0aC5tYXgoZG9jdW1lbnQuYm9keS5vZmZzZXRIZWlnaHQsIGRvY3VtZW50LmRvY3VtZW50RWxlbWVudC5vZmZzZXRIZWlnaHQpLCBNYXRoLm1heChkb2N1bWVudC5ib2R5LmNsaWVudEhlaWdodCwgZG9jdW1lbnQuZG9jdW1lbnRFbGVtZW50LmNsaWVudEhlaWdodCkpKTt9Y2F0Y2goZSl7fTwvc2NyaXB0PjwvaHRtbD4="
I need to change that to a byte array, such as
['P','C','F'] etc, without converting each char to it's respective byte
This is not and edit of: How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
In that question, the string is being converted. It's literally in the title that I do not want to convert
Assuming this is your actual problem description:
I have a base64-encoded string, that I wish to convert to a byte array where each single byte contains the ASCII code for one character from the base64 string.
Then you can very easily do that:
byte[] characterBytes = Encoding.ASCII.GetBytes(input);
Because the characters used in a base64 string are all below Unciode code point 127, they all can be represented in a single byte obtained through Encoding.ASCII.
In fact, if that is your actual problem description, that'd make this question a duplicate of C# Convert a string to ASCII bytes.

UTF8 encoding to base64string and storing to database

I'm currently trying to encode data before storing it to my database.
try
{
byte[] byteEncString = new byte[_strToConvert.Length];
byteEncString = System.Text.Encoding.UTF8.GetBytes(_strToConvert);
string strEncoded = Convert.ToBase64String(byteEncString);
return strEncoded;
}
Does anybody know how long a 15 character string will be after it is encoded via utf8 and base64string? Also, is there a max? My field on sql server is only 50 and i want to limit it in that range. Thoughts?
Well, for one thing there's no point in creating a byte array and then ignoring it - so your code would be simpler as:
byte[] byteEncString = Encoding.UTF8.GetBytes(_strToConvert);
return Convert.ToBase64String(byteEncString);
A .NET char can end up as 3 bytes when UTF-8 encoded1, so that gets to a 45 byte maximum. However, base64 converts 3 bytes to 4 characters, so that gives a 60 character maximum base64 encoded string as the result.
1 This is because any characters not in the basic multilingual plane are represented as a surrogate pair. That pair would end up as 4 bytes, but having taken 2 input characters, so the average "bytes per char" in that case is only 2.

Using C#, what is the most efficient method of converting a string containing binary data to an array of bytes

While there are 100 ways to solve the conversion problem, I am focusing on performance.
Give that the string only contains binary data, what is the fastest method, in terms of performance, of converting that data to a byte[] (not char[]) under C#?
Clarification: This is not ASCII data, rather binary data that happens to be in a string.
UTF8Encoding.GetBytes
I'm not sure ASCIIEncoding.GetBytes is going to do it, because it only supports the range 0x0000 to 0x007F.
You tell the string contains only bytes. But a .NET string is an array of chars, and 1 char is 2 bytes (because a .NET stores strings as UTF16). So you can either have two situations for storing the bytes 0x42 and 0x98:
The string was an ANSI string and contained bytes and is converted to an unicode string, thus the bytes will be 0x00 0x42 0x00 0x98. (The string is stored as 0x0042 and 0x0098)
The string was just a byte array which you typecasted or just recieved to an string and thus became the following bytes 0x42 0x98. (The string is stored as 0x9842)
In the first situation on the result would be 0x42 and 0x3F (ascii for "B?"). The second situation would result in 0x3F (ascii for "?"). This is logical, because the chars are outside of the valid ascii range and the encoder does not know what to do with those values.
So i'm wondering why it's a string with bytes?
Maybe it contains a byte encoded as a string (for instance Base64)?
Maybe you should start with an char array or a byte array?
If you realy do have situation 2 and you want to get the bytes out of it you should use the UnicodeEncoding.GetBytes call. Because that will return 0x42 and 0x98.
If you'd like to go from a char array to byte array, the fastest way would be Marshaling.. But that's not really nice, and uses double memory.
public Byte[] ConvertToBytes(Char[] source)
{
Byte[] result = new Byte[source.Length * sizeof(Char)];
IntPtr tempBuffer = Marshal.AllocHGlobal(result.Length);
try
{
Marshal.Copy(source, 0, tempBuffer, source.Length);
Marshal.Copy(tempBuffer, result, 0, result.Length);
}
finally
{
Marshal.FreeHGlobal(tempBuffer);
}
return result;
}
There is no such thing as an ASCII string in C#! Strings always contain UTF-16. Not realizing this leads to a lot of problems. That said, the methods mentioned before work because they consider the string as UTF-16 encoded and transform the characters to ASCII symbols.
/EDIT in response to the clarification: how did the binary data get in the string? Strings aren't supposed to contain binary data (use byte[] for that).
If you want to go from a string to binary data, you must know what encoding was used to convert the binary data to a string in the first place. Otherwise, you might not end up with the correct binary data. So, the most efficient way is likely GetBytes() on an Encoding subclass (such as UTF8Encoding), but you must know for sure which encoding.
The comment by Kent Boogaart on the original question sums it up pretty well. ;]

Categories

Resources