Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1957896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:56:50+00:00 2026-05-17T07:56:50+00:00

I’m trying to figure out a way to parse out a base64 string from

  • 0

I’m trying to figure out a way to parse out a base64 string from with a larger string.

I have the string "Hello <base64 content> World" and I want to be able to parse out the base64 content and convert it back to a string. "Hello Awesome World"

Answers in C# preferred.

Edit: Updated with a more real example.

--abcdef
\n
Content-Type: Text/Plain;
Content-Transfer-Encoding: base64
\n
<base64 content>
\n
--abcdef--

This is taken from 1 sample. The problem is that the Content…. vary quite a bit from one record to the next.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:56:50+00:00Added an answer on May 17, 2026 at 7:56 am

    In short form you could:

    • split the string on any chars that are not valid base64 data or padding
    • try to convert each token
    • if the conversion succeeds, call replace on the original string to switch the token with the converted value

    In code:

    var delimiters = new char[] { /* non-base64 ASCII chars */ };
    var possibles = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    //need to tweak to include padding chars in matches, but still split on padding?
    //maybe better off creating a regex to match base64 + padding
    //and using Regex.Split?
    
    foreach(var match in possibles)
    {
        try
        {
            var converted = Convert.FromBase64String(match);
            var text = System.Text.Encoding.UTF8.GetString(converted);
            if(!string.IsNullOrEmpty(text))
            {
                value = value.Replace(match, text);
            }
        } 
        catch (System.ArgumentNullException) 
        {
            //handle it
        }
        catch (System.FormatException) 
        {
            //handle it
        }
    }
    

    Without a delimiter though, you can end up converting non-base64 text that happens to be also be valid as base64 encoded text.

    Looking at your example of trying to convert "Hello QXdlc29tZQ== World" to "Hello Awesome World" the above algorithm could easily generate something like "ée¡Ý•ͽµ”¢¹]" by trying to convert the whole string from base64 since there is no delimiter between plain and encoded text.

    Update (based on comments):

    If there are no '\n's in the base64 content and it is always preceded by "Content-Transfer-Encoding: base64\n", then there is a way:

    • split the string on '\n'
    • iterate over all the tokens until a token ends in "Content-Transfer-Encoding: base64"
    • the next token (if there are any) should be decoded (if possible) and then the replacement should be made in the original string
    • return to iterating until out of tokens

    In code:

    private string ConvertMixedUpTextAndBase64(string value)
    {
        var delimiters = new char[] { '\n' };
        var possibles = value.Split(delimiters, 
                                    StringSplitOptions.RemoveEmptyEntries);
    
        for (int i = 0; i < possibles.Length - 1; i++)
        {
            if (possibles[i].EndsWith("Content-Transfer-Encoding: base64"))
            {
                var nextTokenPlain = DecodeBase64(possibles[i + 1]);
                if (!string.IsNullOrEmpty(nextTokenPlain))
                {
                    value = value.Replace(possibles[i + 1], nextTokenPlain);
                    i++;
                }
            }                
        }
        return value;
    }
    
    private string DecodeBase64(string text)
    {
        string result = null;
        try
        {
            var converted = Convert.FromBase64String(text);
            result = System.Text.Encoding.UTF8.GetString(converted);
        }
        catch (System.ArgumentNullException)
        {
            //handle it
        }
        catch (System.FormatException)
        {
            //handle it
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.