Base64ToHex () in the code example

I am reading this sample code :

And since I don't know C #, I decided to postpone this.

At compile time, I have this message:

Main.cs(32,65): error CS1061: Type `string' does not contain a definition for `Base64ToHex' and no extension method `Base64ToHex' of type `string' could be found (are you missing a using directive or an assembly reference?)
Compilation failed: 1 error(s), 0 warnings

      

I have been looking at MSDN and as expected I could not find a link for this method.

Question: Where did this method come from?

ps My code looks like this:

using System.Security.Cryptography;
using System.Text;
using System;

class MainClass
{ 
    public static string Encrypt(string toEncrypt, string key, bool useHashing)
    {
         ..... // same as in post
         .....
    }

    public static void Main( string [] args )
    {
        string key = "secret";

        Console.WriteLine( Encrypt("oscar" + "000", key, true ).Base64ToHex() );
    }

      

}

+1


a source to share


2 answers


If this code has ever been executed, Jeff probably had a String extension method called Base64ToHex. Extension methods allow you to define methods to "extend" other classes, so it appears that the method was actually defined in that class:



public static class ExtensionMethods
{
    public static string Base64ToHex(this string str)
    {
        return ...;
    }
}

      

+1


a source


There is System.String

no method Base64ToHex

. I think you are looking for Convert.FromBase64String and BitConverter.ToString :

string encrypted = Encrypt("oscar" + "000", key, true);
Console.WriteLine(BitConverter.ToString(Convert.FromBase64String(encrypted)));

      




I took a look at your link and I am assuming he wrote an extension helper method that does the same:

public static string Base64ToHex(this string s)
{
    return BitConverter.ToString(Convert.FromBase64String(s));
}

      

+1


a source







All Articles