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

  • SEARCH
  • Home
  • 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 876513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:26:40+00:00 2026-05-15T11:26:40+00:00

I would like to know how to reverse the process of the below DecodeBinaryBase64

  • 0

I would like to know how to reverse the process of the below DecodeBinaryBase64 so that I can have a matching Encode method. In short C# code that if given the output of this method it would return the same string that it took as input.

private static string DecodeBinaryBase64(string stringToDecode)
{
    StringBuilder builder = new StringBuilder();
    foreach (var b in Convert.FromBase64String(stringToDecode))
        builder.Append(string.Format("{0:X2}", b));
    return builder.ToString();
}

Here is an example of an encoded string and its decoded counterpart. The result is a SHA1 hash for a file. The above method is an example of understanding how the decoding works to get to the right string.

ENCODED

/KUGOuoESMWYuDb+BTMK1LaGe7k=

DECODED

FCA5063AEA0448C598B836FE05330AD4B6867BB9

or

0xFCA5063AEA0448C598B836FE05330AD4B6867BB9

Updated to reflect correct SHA1 value thanks to Porges and a fix for hex bug found by Dean ‘codeka’ Hardin.

Implemented Solution

Here is the the implementation I have now, it is from Porges post distilled down to two methods.

private static string EncodeFileDigestBase64(string digest)
{
    byte[] result = new byte[digest.Length / 2];

    for (int i = 0; i < digest.Length; i += 2)
        result[i / 2] = byte.Parse(digest.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);

    if (result.Length != 20)
        throw new ArgumentException("Not a valid SHA1 filedigest.");

    return Convert.ToBase64String(result);
}

private static string DecodeFileDigestBase64(string encodedDigest)
{
    byte[] base64bytes = Convert.FromBase64String(encodedDigest);
    return string.Join(string.Empty, base64bytes.Select(x => x.ToString("X2")));
}  
  • 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-15T11:26:41+00:00Added an answer on May 15, 2026 at 11:26 am

    I don’t believe it’s physically possible. The problem is that string.Format("{0:X}", b) will return either 1 or 2 characters depending on whether the input byte is < 16 or not. And you’ve got no way to know once the string has been joined together.

    If you can modify the DecodeBinaryBase64 method so that it always appends two character for each byte, i.e. by using string.Format("{0:X2}", b) then it will be possible by just taking the input string two characters at a time.

    If you made that change to your DecodeBinaryBase64, then you can use the following to convert back again:

    private static string DecodeBinaryBase64(string stringToDecode)
    {
        StringBuilder builder = new StringBuilder();
        foreach (var b in Convert.FromBase64String(stringToDecode))
            builder.Append(string.Format("{0:X2}", b));
        return "0x" + builder.ToString();
    }
    
    private static string EncodeBinaryBase64(string stringToEncode)
    {
        var binary = new List<byte>();
        for(int i = 2; i < stringToEncode.Length; i += 2)
        {
            string s = new string(new [] {stringToEncode[i], stringToEncode[i+1]});
            binary.Add(byte.Parse(s, NumberStyles.HexNumber));
        }
        return Convert.ToBase64String(binary.ToArray());
    }
    

    (Error checking and so on is missing, though)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.