-3

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+PHNjcmlwdCB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiIHNyYz0iLi4vc2NyaXB0cy9qcXVlcnktMS42LjIubWluLmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9zY3JpcHRzL3BvcHVwLmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9zY3JpcHRzL2hvdmVyYm94LmpzIj48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCIgc3JjPSIuLi9TY3JpcHRzL2pxdWVyeS5wcmludEVsZW1lbnQuanMiPjwvc2NyaXB0PjwvaGVhZD48Ym9keSBvbmxvYWQ9ImlmKHBhcmVudC5zZXRUb29scylwYX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mNoZXIsIEFkdm9jYXRlIG9mIHRoZSBIaWdoIENvdXJ0LCBNZW1iZXIgb2YgdGhlIFByZXRvcmlhIEJhcjsgSkEgRmFyaXMsIFByb2Zlc3NvciBvZiBMYXcsIFVuaXZlcnNpdHkgb2YgU291dGggQWZyaWNhPGJyPjxiPkxhc3QgVXBkYXRlZDogPC9iPk9jdG9iZXIgMjAxMyAtIFNJIDMyLiBQcm9kdWN0IGRldmVsb3BlcjogQ3JhaWdlbiBTdXJhamxhbGw8L3A+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

Community
  • 1
  • 1
TheGeekZn
  • 3,195
  • 7
  • 43
  • 85
  • C# strings use UTF16, so most chars will be 16 bits (and special ones may be more via the use of extended codes). In what way do you want to convert these 16 bit (or more) numbers to bytes? – Matthew Watson Jun 02 '15 at 09:48
  • You cannot convert a 16-bit char to an 8-bit byte without converting - going from 16 bits to 8 bits is the very definition of conversion. Or do you mean mean that you just want to convert each char to two bytes? – Matthew Watson Jun 02 '15 at 09:59
  • 1
    @Semi, okay, I think I kinda understand what you want. I will retract the duplicate closure, but you should know that characters in .NET are *not* bytes. – Frédéric Hamidi Jun 02 '15 at 10:06
  • 2
    Please try to properly explain what you're trying to do then, and especially elaborate on what _"without converting each char to its respective byte"_ is supposed to mean. Show (small!) example input and output. `{ 'P','C','F' }` isn't a byte array, that's a char array. Do you understand the relation between Unicode, characters, encodings and bytes? Do you understand not every character is representable in one byte, depending on the encoding used? See also [Encoding used in cast from char to byte](http://stackoverflow.com/questions/10708548/encoding-used-in-cast-from-char-to-byte). – CodeCaster Jun 02 '15 at 10:12
  • 1
    Looking at your example input, I'm almost certain you should be looking at [`Convert.FromBase64String`](https://msdn.microsoft.com/en-us/library/system.convert.frombase64string%28v=vs.110%29.aspx) – Damien_The_Unbeliever Jun 02 '15 at 10:14

1 Answers1

1

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.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
  • "Because the characters used in base64 are all below Unciode code point 127, they all can be represented in a single byte obtained through Encoding.ASCII." That assumption is incorrect. – leppie Jun 02 '15 at 10:21
  • @leppie which part specifically? For example Wikipedia mentions _"Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format"_. Of course Wiki isn't an authoritive source and one can opt to use snowman `☃` as padding character, I'd say one can safely assume all characters to be representable in ASCII. If not, `Encoding.ASCII.GetBytes()` will print `?` (or 0x3F) for those characters. – CodeCaster Jun 02 '15 at 10:23
  • There is no 1-to-1 mapping with the 'bytes' as you suggest. BASE64 as you guess is 64 possibilities, that needs to map to the 256 possibilities of a byte. `byte[] Convert.FromBase64String(string s)` is what you looking for. – leppie Jun 02 '15 at 10:26
  • @leppie _"the characters used in a base64 string"_ then. Each character used to print a standard base64 string (so not the binary form) can be represented in an ASCII (Unicode code point <= 127). – CodeCaster Jun 02 '15 at 10:27
  • @leppie as I understand OP's question, they do not want to actually decode the base64 string, but obtain it in a different format (being a byte array where each byte holds the according base64 string's character's codepoint). – CodeCaster Jun 02 '15 at 10:29
  • But then you end up with a bloated array that is ~4 times bigger than it should be. What would be the use of this? – leppie Jun 02 '15 at 10:31
  • @leppie I don't know, that's why [I asked OP](http://stackoverflow.com/questions/30592617/change-string-to-byte-array-without-conversion/30593329#comment49254829_30592617) who hasn't reponded yet. – CodeCaster Jun 02 '15 at 10:31