-2

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

6
  • 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? Commented Jun 2, 2015 at 9: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? Commented Jun 2, 2015 at 9: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. Commented Jun 2, 2015 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. Commented Jun 2, 2015 at 10:12
  • 1
    Looking at your example input, I'm almost certain you should be looking at Convert.FromBase64String Commented Jun 2, 2015 at 10:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

"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 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.
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 "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).
@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).
|
0

If you used a special CodePage during the making of the original text, you should use like:

byte[] characterBytes = Encoding.GetEncoding(1252).GetBytes(inString);

On the other direction you can use:

string outString = Encoding.GetEncoding(1252).GetString(characterBytes);

It can be important to use the same code page as the media, such as the code page of the SQL Server database, to avoid additional conversion. This is also good for storing a .zip or binary file content into an SQL database text field. If you want to be sure that all binary is converted to character, you should use CodePage = 1252.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.