You have to use Convert.FromBase64String to turn a Base64 encoded string into a byte[].

Answer from Zruty on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/csharp โ€บ difference between storing a base64 in a string or byte[]
r/csharp on Reddit: Difference between storing a base64 in a string or byte[]
October 5, 2021 -

Hi everyone so my question is very simple.

Are there any performance gains when storing a base64 as a byte[] or string type?

Imagine the following scenario

Example piece of code

PS: Sorry for my bad english, it's not my native language.

b64 - A command-line Base64 encoder and decoder in C Apr 22, 2025
r/C_Programming
last yr.
I created a base64 library Dec 28, 2024
r/C_Programming
last yr.
Base64 string to 32 bit float array Oct 21, 2019
r/learnprogramming
6y ago
More results from reddit.com
Discussions

Base64-decoding binary data to byte array
Sign up ยท Log in ยท Reset your password ยท Create account ยท Reset password ยท Create a new account More on forum.nim-lang.org
๐ŸŒ forum.nim-lang.org
January 3, 2021
How to convert a base 64 string to byte array in C++? - Post.Byes
I have a wstring that has a base 64 string. I need to extract a byte array from this base 64 string. How can I do this? I am implementing a COM Server in C++. Please help More on post.bytes.com
๐ŸŒ post.bytes.com
September 24, 2010
C++ Base64 to byte array
Find answers to C++ Base64 to byte array from the expert community at Experts Exchange More on experts-exchange.com
๐ŸŒ experts-exchange.com
May 15, 2009
Base64 Encoded String to byte array C# Failing - Stack Overflow
We are receiving a base64 encoded string from a external application in the form body and below is the code that we are using to decode the string into byte array, however we are getting an exception More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
ASPSnippets
aspsnippets.com โ€บ Articles โ€บ 2087 โ€บ Convert-Base64-string-to-Byte-Array-using-C-and-VBNet
Convert Base64 string to Byte Array using C# and VB.Net [2026]
January 29, 2026 - Learn how to convert Base64 string to Byte Array using C# and VBNet Contains Examples, Screenshots and Free Tested Source Code for download.
๐ŸŒ
Nim Forum
forum.nim-lang.org โ€บ t โ€บ 7359
Base64-decoding binary data to byte array
January 3, 2021 - Sign up ยท Log in ยท Reset your password ยท Create account ยท Reset password ยท Create a new account
๐ŸŒ
Post.Byes
post.bytes.com โ€บ home โ€บ forum โ€บ topic
How to convert a base 64 string to byte array in C++? - Post.Byes
September 24, 2010 - I want to now extract the byte array out of this string in C++. My objective is to get back the byte array with elements 1 and 2 which is the original data in my COM Server. My function in COM Server looks like this: ... NotifyData(BSTR myData) { wstring myDataBytes = myData; //base64 string containing "AQI=" byte *pArr = new byte[2]; memset(pArr ,0,2); //Do something to get the original byte array into pArr such that pArr[0] = 1 and pArr[1] = 2 }
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 24412494 โ€บ C-Base64-to-byte-array.html
Solved: C++ Base64 to byte array | Experts Exchange
May 15, 2009 - I'd suggest to - instead of resinventing the wheel - use a ready-made class for that, see e.g. http://www.codeproject.com/KB/cpp/base64coding.aspx ("Base64 Encoding And Decoding").
๐ŸŒ
Cryptii
cryptii.com โ€บ pipes โ€บ base64-to-hex
Base64 to hex: Encode and decode bytes online - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.
Find elsewhere
๐ŸŒ
Leonardotamiano
blog.leonardotamiano.xyz โ€บ tech โ€บ base64
Base64 Encoder in C - Leonardo Tamiano
In this line we're encoding the ... second byte. The masks here are used to extract these sequence of bits from the original number, and the shift operations are used to place correctly the bits. Finally, the OR operation is used to combine the two sequences of bits in order to obtain the final and correct value. This value is then used as an index into the BASE64 array, which gives ...
Top answer
1 of 2
3

Thank you all for the response.

I got this working, after studying how the base64 encoding works.

below is the code that i used to fix it.

var input = new StreamReader(Request.InputStream).ReadToEnd();
        var inputText = "PHBheW1lbnRSZXNwb25zZT48cmVzcG9uc2VDb2RlPjAwMDA8L3Jlc3BvbnNlQ29kZT48cmVzcG9uc2VDb2RlVGV4dD4wLVN1Y2Nlc3NmdWw8L3Jlc3BvbnNlQ29kZVRleHQ+PHJlc3BvbnNlU3VtbWFyeT5HUkVFTjwvcmVzcG9uc2VTdW1tYXJ5PjxwYXltZW50RXZlbnRJZGVudGlmaWVyPlRYTiAzNjM5PC9wYXltZW50RXZlbnRJZGVudGlmaWVyPjxMaXN0Pjxjb21wb25lbnRJRD5UWE4gMzYzOTwvY29tcG9uZW50SUQ+PGNsaWVudElEPkdPVERJU0UwNjwvY2xpZW50SUQ+PGJhbmtBdXRoQ29kZT5UOjEyMzQ8L2JhbmtBdXRoQ29kZT48YnV5bmV0VHhuSUQ+Mzc1PC9idXluZXRUeG5JRD48L0xpc3Q+PHBheW1lbnRJbnN0cnVtZW50UmVmPjwvcGF5bWVudEluc3RydW1lbnRSZWY+PG1hc2tlZENhcmROdW1iZXI+KioqKioqKioqKioqOTY4NjwvbWFza2VkQ2FyZE51bWJlcj48Y2FyZFR5cGU+TUFTVEVSQ0FSRDwvY2FyZFR5cGU+PGV4cGlyeURhdGU+MDMvMjAxNzwvZXhwaXJ5RGF0ZT48Y3VzdG9tRGF0YT4mbHQ7IVtDREFUQVsmbHQ7P3htbCB2ZXJzaW9uPSIxLjAiIGVuY29kaW5nPSJVVEYtOCI_Jmd0Ow0KJmx0O1RoaXN0bGVDdXN0b21EYXRhIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiJmd0Ow0KICAmbHQ7T3JkZXJJZCZndDtCVFBQMzYzOSZsdDsvT3JkZXJJZCZndDsNCiAgJmx0O0Ftb3VudCZndDsxMjAmbHQ7L0Ftb3VudCZndDsNCiZsdDsvVGhpc3RsZUN1c3RvbURhdGEmZ3Q7XV0mZ3Q7PC9jdXN0b21EYXRhPjwvcGF5bWVudFJlc3BvbnNlPg";
        inputText = ValidateBase64EncodedString(inputText);

        byte[] decodedBytes = Convert.FromBase64String(inputText);

        string xml = Encoding.UTF8.GetString(decodedBytes);

       private static string ValidateBase64EncodedString(string inputText)
        {
            string stringToValidate = inputText;
            stringToValidate = stringToValidate.Replace('-', '+'); // 62nd char of encoding
            stringToValidate = stringToValidate.Replace('_', '/'); // 63rd char of encoding
            switch (stringToValidate.Length % 4) // Pad with trailing '='s
            {
                case 0: break; // No pad chars in this case
                case 2: stringToValidate += "=="; break; // Two pad chars
                case 3: stringToValidate += "="; break; // One pad char
                default:
                    throw new System.Exception(
             "Illegal base64url string!");
            }

            return stringToValidate;
        }
2 of 2
0

It's because your custom data is invalid. In that Base64 encoded string you have a raw [CDATA] which is most likely not encoded correctly.

notice that if i take the first 684 characters of your string, it converts properly to:

<paymentResponse><responseCode>0000</responseCode><responseCodeText>0-Successful</responseCodeText><responseSummary>GREEN</responseSummary><paymentEventIdentifier>TXN 3639</paymentEventIdentifier><List><componentID>TXN 3639</componentID><clientID>GOTDISE06</clientID><bankAuthCode>T:1234</bankAuthCode><buynetTxnID>375</buynetTxnID></List><paymentInstrumentRef></paymentInstrumentRef><maskedCardNumber>************9686</maskedCardNumber><cardType>MASTERCARD</cardType><expiryDate>03/2017</expiryDate><customData>&

but all hell breaks loose when we get to the customData tag, this I suppose, contains the CDATA xml content, which is probably not base64 encoded.

So either leave the CDATA content out, or encode it properly before adding it to the final string, to fix the issue

๐ŸŒ
PerlMonks
perlmonks.org
Base64 and byte arrays
PerlMonks graciously bestowed by Tim Vroom. PerlMonks went on a couple dates, and then decided to shack up with The Perl Foundation. Donate to TPF!
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.convert.tobase64string
Convert.ToBase64String Method (System) | Microsoft Learn
Dim bytes(arr.Length * 4 - 1) As Byte For ctr As Integer = 0 To arr.Length - 1 Array.Copy(BitConverter.GetBytes(arr(ctr)), 0, bytes, ctr * 4, 4) Next ' Encode the byte array using Base64 encoding Dim base64 As String = Convert.ToBase64String(bytes) Console.WriteLine("The encoded string: ") For ctr As Integer = 0 To base64.Length \ 50 - 1 Console.WriteLine(base64.Substring(ctr * 50, If(ctr * 50 + 50 <= base64.Length, 50, base64.Length - ctr * 50))) Next Console.WriteLine() ' Convert the string back to a byte array.
๐ŸŒ
Lua Ternary
nachtimwald.com โ€บ posts โ€บ base64 encode and decode in c
Base64 Encode and Decode in C | John's Blog
November 18, 2017 - This gives us a 4:3 ratio, meaning there is 33% overhead for base64. To get the number of base64 characters needed for a given binary data blob, take the length of input and round up to the nearest multiple of 3. Then, divide by 3 to get the number of 3 byte blocks.
๐ŸŒ
Base64
base64.dev โ€บ home โ€บ articles โ€บ base64 in c
Base64 Encoding & Decoding in C โ€” base64.dev
July 3, 2026 - The expression (len + 2) / 3 is the standard integer-arithmetic trick for computing ceil(len / 3) without floating point. Multiplying that by 4 gives the exact encoded length for any input, including the padding characters.
๐ŸŒ
Unreal Engine
forums.unrealengine.com โ€บ development โ€บ programming & scripting โ€บ c++
How to convert a string of Base64 to Byte Array? - C++ - Epic Developer Community Forums
September 29, 2021 - Hi, I am trying to convert a Base64 in string to byte arrays. Any ideas on how can I implement it in c++? Thank you!!
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.convert.frombase64string
Convert.FromBase64String(String) Method (System) | Microsoft Learn
Dim bytes(arr.Length * 4 - 1) As Byte For ctr As Integer = 0 To arr.Length - 1 Array.Copy(BitConverter.GetBytes(arr(ctr)), 0, bytes, ctr * 4, 4) Next ' Encode the byte array using Base64 encoding Dim base64 As String = Convert.ToBase64String(bytes) Console.WriteLine("The encoded string: ") For ctr As Integer = 0 To base64.Length \ 50 - 1 Console.WriteLine(base64.Substring(ctr * 50, If(ctr * 50 + 50 <= base64.Length, 50, base64.Length - ctr * 50))) Next Console.WriteLine() ' Convert the string back to a byte array.
๐ŸŒ
Elixir Forum
elixirforum.com โ€บ t โ€บ convert-base64-string-to-byte-string โ€บ 15092
Convert base64 string to byte string | Elixir Forum
July 11, 2018 - Most of the time, the binary consumes much less memory, and if you are randomly reading bytes from it, its also much faster. Also writing it to a file is probably faster, as it can be read from continous memory, while writing from a list would mean to follow a pointer for each next byte.
๐ŸŒ
Medium
medium.com โ€บ c-sharp-programming โ€บ mastering-base64-encoding-and-decoding-in-c-803805c388d0
Mastering Base64 Encoding and Decoding in C# | by Valentyn Osidach ๐Ÿ“š | Any Programming | Medium
September 11, 2024 - Base64 encoding and decoding are fundamental skills in C# programming, enabling the efficient handling and transport of binary data in textual formats. Whether youโ€™re storing binary data in JSON, embedding it in URLs, or ensuring data integrity, Base64 provides a reliable and easy-to-use solution.
๐ŸŒ
Particle
community.particle.io โ€บ firmware
Base64 decode string to byte array - Firmware - Particle
March 6, 2015 - I feel like I have exhausted every resource online trying to find a routine to decode a Base64 encoded String. I need a method I can pass a String to and have it return something like an unsiged char[] or other form of โ€ฆ
๐ŸŒ
Cryptii
cryptii.com โ€บ pipes โ€บ binary-to-base64
Binary to base64: Convert between bytes and base64 - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.