locked
Decoding Base64 String RRS feed

  • Question

  • User-501297529 posted

    I am trying to decode a base64 in C#. I honestly have no idea how to run this in Visual Studio.

    Here is the string:

    MjIwNDY2OTZlNjE2ZTYzNjk2MTZjMjA0NDcyNjU2MTZkNzM=

    and here is the code i have so far, I know that I'm missing some code so I need assistance with this:

    using System;
    using System.Text;
    
    public class MyClass {
        
        byte[] decodedBytes = Convert.FromBase64String(embedCode);
        string decodedText = Encoding.UTF8.GetString(decodedBytes);
    }
    
    Wednesday, August 7, 2019 4:03 PM

Answers

  • User475983607 posted

    Your code is so far off the mark that I'm not sure how to help you.  Let's try this...

    Create a new folder in your project and name it "Utilities".  Right click the Utilities folder, highlight add, and select class.  Name the class Base64Utility and copy the two lines of code that decodes the base64 string,  It should look similar to the following except your namespace will start with the name of your project not MvcApiDb which is the name of my project.

    using System;
    using System.Text;
    
    namespace MvcApiDb.Utilities
    {
        public class Base64Utility
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }
    }

    Now you can use the class anywhere in your project.  For example, an action...

            public IActionResult Index()
            {
                string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
                string results = Utilities.Base64Utility.DecodeBase64ToString(embedCode);
                return Json(results);
            }

    results

    // 20190807192044
    // https://localhost:44327/
    
    "Hello World"

    The Base64Utility class has a static method DecodeBase64ToString which means the method is not an instance member and can be called directly from the class. 

    Classes are the basic building blocks in many languages.  I recommend that you read the link in my first post and make an effort to learn the concept.  There is a lot of information so be sure to click the links on the left.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 7, 2019 11:29 PM
  • User-501297529 posted

    mgebhard

    bootzilla

    One issues is when I get the results using the the embedded code that I have it gives me a bunch of numbers/letters '48656c70696e67204d656d62657273204c6976652054686569722046696e616e6369616c204' instead of text like a Hello World. There is one step I'm missing but not sure what that is.

    You have to consider that the forum members have no idea what object is Base64 encoded in your example. 

    Since your original code sample is converting from Base64 to a string, I created a "Hello World" example. That way we can verify the operation is successful or not.

    If your Base64 object is an image then covert to an image not a string.  

    It's not an image. It should embed into plain text but for some reason it is giving me that combo of numbers and letters.

    I even tried this creating a console app and it gives me the same combo:

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main()
            {
                string encodedText = "A0YzY5NzY2NTIwNTQ2ODY1Njk3MjIwNDY2OTZlNjE2ZTYzNjk2MTZjMjA0NDcyNjU2MTZkNzM=";
    
                var encodedTextBytes = Convert.FromBase64String(encodedText);
    
                string plainText = Encoding.UTF8.GetString(encodedTextBytes);
    
                Console.WriteLine("Encoded Text : {0}", encodedText);
                Console.WriteLine("Plain Text : {0}", plainText);
                Console.ReadKey();
            }
        }
    }

    It should give me a phrase like 'Hello World..' Is there a step I'm missing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 2:33 PM

All replies

  • User475983607 posted

    Your code is correct but the class pattern is way off.

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    byte[] decodedBytes = Convert.FromBase64String(embedCode);
    string decodedText = Encoding.UTF8.GetString(decodedBytes);
    
    Console.WriteLine(decodedText);

    Working class example.

        public class MyClass
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }

    Implementation

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    Console.WriteLine(MyClass.DecodeBase64ToString(embedCode));

    See the C# Programming guide to learn about classes and common patterns.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    Wednesday, August 7, 2019 5:46 PM
  • User-501297529 posted

    Your code is correct but the class pattern is way off.

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    byte[] decodedBytes = Convert.FromBase64String(embedCode);
    string decodedText = Encoding.UTF8.GetString(decodedBytes);
    
    Console.WriteLine(decodedText);

    Working class example.

        public class MyClass
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }

    Implementation

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    Console.WriteLine(MyClass.DecodeBase64ToString(embedCode));

    See the C# Programming guide to learn about classes and common patterns.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    This is what I have now:

    {
        public static string DecodeBase64ToString(string embedCode)
        {
        
             embedCode = "SGVsbG8gV29ybGQ="; //Hello World
            byte[] decodedBytes = Convert.FromBase64String(embedCode);
            string decodedText = Encoding.UTF8.GetString(decodedBytes);
    
            Console.WriteLine(decodedText);
        }
    }
    

    I get an error  'MyClass.DecodeBase64ToString(string)': not all code paths return a value Base64Encode.

     
    Also I have this in a class file currently, so when I run it where do I go to see the decodedText? Should I put this in a different file? 

    Wednesday, August 7, 2019 6:36 PM
  • User475983607 posted

    I provided a working class.  Not sure why you did not copy the working class from my first post.

        public class MyClass
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }

    Implementation.

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    string result = MyClass.DecodeBase64ToString(embedCode)

    Wednesday, August 7, 2019 6:45 PM
  • User-501297529 posted

    I provided a working class.  Not sure why you did not copy the working class from my first post.

        public class MyClass
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }

    Implementation.

    string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    string result = MyClass.DecodeBase64ToString(embedCode)

    I'm stuck on the implementation part. I'm missing something but not sure what it is. I get 'embedCode does not exist'

    public class MyClass
    {
        public static string DecodeBase64ToString(string embedCode)
        {
            byte[] decodedBytes = Convert.FromBase64String(embedCode);
            return Encoding.UTF8.GetString(decodedBytes);
             
    
        }
        public MyClass()
            {
            embedCode = "SGVsbG8gV29ybGQ="; //Hello World
            string result = MyClass.DecodeBase64ToString(embedCode);
        }
    
    }

    Wednesday, August 7, 2019 8:59 PM
  • User753101303 posted

    Hi,

    You are trying to use a variable without declaring it first.

    string embedCode = "SGVsbG8gV29ybGQ=";

    And try to write code directly into the class.

    Wednesday, August 7, 2019 9:17 PM
  • User475983607 posted

    Your code is so far off the mark that I'm not sure how to help you.  Let's try this...

    Create a new folder in your project and name it "Utilities".  Right click the Utilities folder, highlight add, and select class.  Name the class Base64Utility and copy the two lines of code that decodes the base64 string,  It should look similar to the following except your namespace will start with the name of your project not MvcApiDb which is the name of my project.

    using System;
    using System.Text;
    
    namespace MvcApiDb.Utilities
    {
        public class Base64Utility
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }
    }

    Now you can use the class anywhere in your project.  For example, an action...

            public IActionResult Index()
            {
                string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
                string results = Utilities.Base64Utility.DecodeBase64ToString(embedCode);
                return Json(results);
            }

    results

    // 20190807192044
    // https://localhost:44327/
    
    "Hello World"

    The Base64Utility class has a static method DecodeBase64ToString which means the method is not an instance member and can be called directly from the class. 

    Classes are the basic building blocks in many languages.  I recommend that you read the link in my first post and make an effort to learn the concept.  There is a lot of information so be sure to click the links on the left.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 7, 2019 11:29 PM
  • User665608656 posted

    Hi bootzilla,

    I'm stuck on the implementation part. I'm missing something but not sure what it is. I get 'embedCode does not exist'

    This error shows that you haven't define the variable named 'embedCode'.

    You can refer to this link : How to make method call another one in classes?

    Is you do this thing in webform , I have made an example about this , you can follow this :

    I have an aspx page named WebForm_0808_2158562.aspx and a class named MyClass .

    Theres is a button in WebForm_0808_2158562.aspx. In the button click event, define embedCode, and then call DecodeBase64ToString in MyClass to accept the decoded value .

    MyClass.cs:

      public class MyClass
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }

    WebForm_0808_2158562.aspx.cs:

      protected void Button1_Click(object sender, EventArgs e)
            {
                string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
    
                string result = MyClass.DecodeBase64ToString(embedCode);
                Label1.Text = result;
            }

    Here is the result :

    Best Regards,

    YongQing.

    Thursday, August 8, 2019 5:26 AM
  • User-501297529 posted

    Your code is so far off the mark that I'm not sure how to help you.  Let's try this...

    Create a new folder in your project and name it "Utilities".  Right click the Utilities folder, highlight add, and select class.  Name the class Base64Utility and copy the two lines of code that decodes the base64 string,  It should look similar to the following except your namespace will start with the name of your project not MvcApiDb which is the name of my project.

    using System;
    using System.Text;
    
    namespace MvcApiDb.Utilities
    {
        public class Base64Utility
        {
            public static string DecodeBase64ToString(string embedCode)
            {
                byte[] decodedBytes = Convert.FromBase64String(embedCode);
                return Encoding.UTF8.GetString(decodedBytes);
            }
        }
    }

    Now you can use the class anywhere in your project.  For example, an action...

            public IActionResult Index()
            {
                string embedCode = "SGVsbG8gV29ybGQ="; //Hello World
                string results = Utilities.Base64Utility.DecodeBase64ToString(embedCode);
                return Json(results);
            }

    results

    // 20190807192044
    // https://localhost:44327/
    
    "Hello World"

    The Base64Utility class has a static method DecodeBase64ToString which means the method is not an instance member and can be called directly from the class. 

    Classes are the basic building blocks in many languages.  I recommend that you read the link in my first post and make an effort to learn the concept.  There is a lot of information so be sure to click the links on the left.

     

    OK thanks for this. As I stated doing something like this is very foreign to me but now I'm starting to understand.

    One issues is when I get the results using the the embedded code that I have it gives me a bunch of numbers/letters '48656c70696e67204d656d62657273204c6976652054686569722046696e616e6369616c204' instead of text like a Hello World. There is one step I'm missing but not sure what that is.

    Thursday, August 8, 2019 1:53 PM
  • User475983607 posted

    bootzilla

    One issues is when I get the results using the the embedded code that I have it gives me a bunch of numbers/letters '48656c70696e67204d656d62657273204c6976652054686569722046696e616e6369616c204' instead of text like a Hello World. There is one step I'm missing but not sure what that is.

    You have to consider that the forum members have no idea what object is Base64 encoded in your example. 

    Since your original code sample is converting from Base64 to a string, I created a "Hello World" example. That way we can verify the operation is successful or not.

    If your Base64 object is an image then covert to an image not a string.  

    Thursday, August 8, 2019 2:06 PM
  • User-501297529 posted

    mgebhard

    bootzilla

    One issues is when I get the results using the the embedded code that I have it gives me a bunch of numbers/letters '48656c70696e67204d656d62657273204c6976652054686569722046696e616e6369616c204' instead of text like a Hello World. There is one step I'm missing but not sure what that is.

    You have to consider that the forum members have no idea what object is Base64 encoded in your example. 

    Since your original code sample is converting from Base64 to a string, I created a "Hello World" example. That way we can verify the operation is successful or not.

    If your Base64 object is an image then covert to an image not a string.  

    It's not an image. It should embed into plain text but for some reason it is giving me that combo of numbers and letters.

    I even tried this creating a console app and it gives me the same combo:

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main()
            {
                string encodedText = "A0YzY5NzY2NTIwNTQ2ODY1Njk3MjIwNDY2OTZlNjE2ZTYzNjk2MTZjMjA0NDcyNjU2MTZkNzM=";
    
                var encodedTextBytes = Convert.FromBase64String(encodedText);
    
                string plainText = Encoding.UTF8.GetString(encodedTextBytes);
    
                Console.WriteLine("Encoded Text : {0}", encodedText);
                Console.WriteLine("Plain Text : {0}", plainText);
                Console.ReadKey();
            }
        }
    }

    It should give me a phrase like 'Hello World..' Is there a step I'm missing?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 8, 2019 2:33 PM
  • User475983607 posted

    It should give me a phrase like 'Hello World..' Is there a step I'm missing?

    No, Base64 is pretty simple.   I'm guessing there is another level of encryption that must be decrypted before converting to text.  Again, there's no much we can do for you if you do not know what you're Base64 decoding.

    Thursday, August 8, 2019 2:53 PM
  • User-501297529 posted

    bootzilla

    It should give me a phrase like 'Hello World..' Is there a step I'm missing?

    No, Base64 is pretty simple.   I'm guessing there is another level of encryption that must be decrypted before converting to text.  Again, there's no much we can do for you if you do not know what you're Base64 decoding.

    I figured it out. I had to convert the hex code into ASCII Text using this chart on this website and voila!

    https://www.rapidtables.com/convert/number/hex-to-ascii.html

    Thursday, August 8, 2019 3:00 PM
  • User753101303 posted

    Great but it seems really not needed.

    The purpose of the base 64 encoding is to be able to represent binary data with "printable characters". Going through an hexadecimal string representation seems entirely useless (or it was not done on purpose ?).

    Thursday, August 8, 2019 3:13 PM
  • User-501297529 posted

    Great but it seems really not needed.

    The purpose of the base 64 encoding is to be able to represent binary data with "printable characters". Going through an hexadecimal string representation seems entirely useless (or it was not done on purpose ?).

    Sort of on purpose. I had to figure this out where I work. Our company motto is on the wall in our department in base64. So in order to convert it there are a couple of layers: from Base64 to hex and from hex to ascii.

    Thursday, August 8, 2019 4:48 PM