Tutorial :Base64ToHex() on code sample


I'm reading this code sample:

And since I don't know C#, I decided to give it a go.

While compiling I've got 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've look in MSDN and as expected I didn't find a reference for that method.

Question: Where did that method came from?

p.s. My code looks like:

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() );      }  



If that code ever complied, Jeff probably had an extension method on String called "Base64ToHex". Extension methods allow you to define methods to "extend" other classes, such that it appears that the method was actually defined in that class:

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


There is no Base64ToHex method in System.String. I think you're 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'm guessing he wrote a helper extension method that does the same:

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »