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+Q2l2aWwgUHJvY2VkdXJlIGluIE1hZ2lzdHJhdGVzJyBDb3VydHM8L3RpdGxlPjxsaW5rIHR5cGU9InRleHQvY3NzIiByZWw9InN0eWxlc2hlZXQiIGhyZWY9Ii4uL1N0eWxlcy9CV0NvbW1vbi5jc3MiPjxsaW5rIHR5cGU9InRleHQvY3NzIiByZWw9InN0eWxlc2hlZXQiIGhyZWY9Ii4uL1N0eWxlcy9TaXRlQ29tbW9uLmNzcyI+PHNjcmlwdCB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiIHNyYz0iLi4vc2NyaXB0cy9qcXVlcnktMS42LjIubWluLmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9zY3JpcHRzL3BvcHVwLmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9zY3JpcHRzL2hvdmVyYm94LmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9TY3JpcHRzL2pxdWVyeS5wcmludEVsZW1lbnQuanMiPjwvc2NyaXB0PjwvaGVhZD48Ym9keSBvbmxvYWQ9ImlmKHBhcmVudC5zZXRUb29scylwYXJlbnQuc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IEFkdm9jYXRlIG9mIHRoZSBIaWdoIENvdXJ0LCBNZW1iZXIgb2YgdGhlIFByZXRvcmlhIEJhcjsgSkEgRmFyaXMsIFByb2Zlc3NvciBvZiBMYXcsIFVuaXZlcnNpdHkgb2YgU291dGggQWZyaWNhPGJyPjxiPkxhc3QgVXBkYXRlZDogPC9iPk9jdG9iZXIgMjAxMyAtIFNJIDMyLiBQcm9kdWN0IGRldmVsb3BlcjogQ3JhaWdlbiBTdXJhamxhbGw8L3A+PHA+PGJyPjxicj48L3A+PHA+PGJyPjxicj48L3A+PC9kaXY+PGRpdiBpZD0iY29udGV4dE1lbnUiPjwvZGl2PjwvYm9keT48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCI+dHJ5e3dpbmRvdy5wYXJlbnQuc2V0RnJhbWVIZWlnaHQoIE1hdGgubWF4KE1hdGgubWF4KGRvY3VtZW50LmJvZHkuc2Nyb2xsSGVpZ2h0LCBkb2N1bWVudC5kb2N1bWVudEVsZW1lbnQuc2Nyb2xsSGVpZ2h0KSwgTWF0aC5tYXgoZG9jdW1lbnQuYm9keS5vZmZzZXRIZWlnaHQsIGRvY3VtZW50LmRvY3VtZW50RWxlbWVudC5vZmZzZXRIZWlnaHQpLCBNYXRoLm1heChkb2N1bWVudC5ib2R5LmNsaWVudEhlaWdodCwgZG9jdW1lbnQuZG9jdW1lbnRFbGVtZW50LmNsaWVudEhlaWdodCkpKTt9Y2F0Y2goZSl7fTwvc2NyaXB0PjwvaHRtbD4="
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.
Related
I am getting a string of zeros and ones from a client API request. They are of a set length (28, in this case) and I want to convert them to a byte[] or something similar, with the goal of storing these in SQL via EF Core and later using bitwise operators to compare them.
I can't seem to wrap my head around this one. I'm seeing a lot of posts/questions about converting characters to byte arrays, or byte arrays to strings, neither of which is what I need.
I need a "00111000010101010" to become a literal binary 00111000010101010 I can use a ^ on.
Leading zeros would be fine if necessary, I think the length might be forced to be a multiple of 8?
You can binary string convert to an integer easily with this:
string source = "00111000010101010";
int number = Convert.ToInt32(source, 2); // The `2` is "base 2"
That gives: 28842.
Then you can go one step further an convert to a byte array, if needed.
byte[] bytes = BitConverter.GetBytes(number);
I have some encrypting (and decrypting) functions that take any int value and return also any int value (it uses some xor operations with large prime numbers under the hood):
public interface IIntEncryption {
public int Encrypt(int value);
public int Decrypt(int encryptedValue);
}
Now I want to use it to encrypt string. My idea is to take each char from the string, convert it to int, encrypt with the function above and then convert back to char. The problem is that not every int value is valid character value afaik. So casting int to char won't work. So I am thinking that I will need to convert int to two valid characters somehow and then when decrypting convert each pair of two characters. So basically I am looking for following functions:
public interface IStringEncryption {
public string Encrypt(string str, IIntEncryption intEncryption);
public string Decrypt(string encryptedStr, IIntEncryption intEncryption);
}
I tried many ways but I can't figure out how to encrypt/decrypt array if integers into string representation. Finally I want it to be base64 encoded string.
I have some encrypting (and decrypting) functions that take any int value and return also any int value (it uses some xor operations with large prime numbers under the hood):
This is fine, assuming you are only doing this for fun/education. It is generally frowned upon creating your own encryption algorithms or implementations for anything where security is needed.
My idea is to take each char from the string, convert it to int, encrypt with the function above and then convert back to char
This would not be fine, or at least a very cumbersome way to do it. As you surmised you could fit two utf16 characters in a 32-bit int. But you will still have the same problem converting to a string again, since not all 16 bit values are valid utf16 characters.
A better solution would be to convert your string to a byte-array using some kind of encoding. You can then convert pairs of 4 bytes to int32s, encrypt, convert back to a byte array, and use something like base64 to convert the bytes back to a string.
This might sound a bit complicated, and it is. So most real implementations just work with byte-arrays directly, typically splitting them into some larger chunk-size internally.
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);
I am being passed a byte array representing a pdf as part of an xml node.
the byte array int the xml looks like this
<Document>
Xh0XQo+PgovVHlwZSAvDAwIG4gCjAwMDAxNTc0MjkgMDAwMDAgbiAKMDAwMDE1ODQ1NSAwMDAwMCBuIAowMDAwMTU5MzY1IDAwMDAwIG4gCjAwMDAxNTk2MjEgMDATg5MyAwMDAwMCBuIAowMDAwMTYwMTQzIDAwMDAwIG4gCjAwMMDE2MDYzNSAwMDAwMCBuIAowMDAwMTYwODk5IDAwMDAwIG4gCjAwMDAxNNTkgMDAwMDAgbiAwMDE2NDkxMiAwMDAwMCBuIAowMDAwMTY1MTwMDAwIG4gCjAwMDAxNjU0MzYgMDAwMDE2NTUyMyAwMDAwMCBuIAowMDAwMTY1NzA5IDAwMDAwIG4gCjAwMDAxNjU5MjcgMDAwMDAgbiAKMDA4MTg3OSAwMDAwMCBuIAowMMTgxOTc4IDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSAxMTMxCi9Sb290IDEgMCBSCi9JbmZvIDMgMCBSCi9JRCBbPDgyMTQwQURDM0QwOTRCREZBODI2MjM4Q0VBM0YxODA3PiA8ODIxNDBBREMzRDA5NEJERkE4MjYyMzhDRUEzRjE4MDc+XQovRW5jcnlwdCA0IDAgUgo+PgpzdGFydHhyZWYKMTgyMDEzCiUlRU9GCg...........</Document>
So I first copy the bytearray into a string variable .
string pdfbyte = GetNodeUsingXpath(xpath.....);
Now I would like to cast this pdfbyte into a byte array.
byte[] output = (byte[])pdfbyte;
byte[] output = byte.parse(pdfbyte);
These dont work.
I have looked online but could not find a simple solution to cast a byte array stored in a string variable to a byte array. Any pointers would be helpful.
Basically, I would like to copy the bytearray that is being sent as part of the xml into a
byte array variable.
If you've got binary data within an XML document, I would hope that it's base64-encoded. Text data and binary data are different, and you shouldn't be trying to store arbitrary binary data in a string directly.
If it is, you can just use:
string base64 = GetNodeUsingXpath(xpath.....);
byte[] output = Convert.FromBase64String(base64);
You may find you need to trim the string first:
byte[] output = Convert.FromBase64String(base64.Trim());
If it's not base64, you'll need to look carefully at what the text looks like. It may be hex instead, although that's not terribly likely. If someone has just use Encoding.GetString(bytes) to start with, then that code will need to be fixed, and it's almost guaranteed to lose data.
EDIT: Now that we can see some of the data, it does indeed look like it's base64.
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. ;]