Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert from Base 64 to hexadecimal value

Convert from Base 64 to hexadecimal value

Scheduled Pinned Locked Moved C#
csharpxmltutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)

    S P 2 Replies Last reply
    0
    • N NJdotnetdev

      I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.

      N 1 Reply Last reply
      0
      • S SledgeHammer01

        I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Apologies for any confusion.. I just gave sample values. I am trying to get the values you mentioned programmatically.

        1 Reply Last reply
        0
        • N NJdotnetdev

          I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#: ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA="); string hex = BitConverter.ToString(bytes); hex.Replace("-", String.Empty); The 'hex' output is in the following format: 4D5459334D5445344D4467413D However, required output should be in the format: #44ff11 (example)

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I use Encoding.GetString ( System.Convert.FromBase64String ( Source ) ), which yields TmpVME1EZ0E9 from your sample data.

          N 1 Reply Last reply
          0
          • P PIEBALDconsult

            I use Encoding.GetString ( System.Convert.FromBase64String ( Source ) ), which yields TmpVME1EZ0E9 from your sample data.

            N Offline
            N Offline
            NJdotnetdev
            wrote on last edited by
            #5

            And hex should be "4E6A55304D4467413D"

            P 1 Reply Last reply
            0
            • N NJdotnetdev

              And hex should be "4E6A55304D4467413D"

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I'm not following you. From TmpVME1EZ0E9 I get 546D70564D4531455A304539 The sequence you state yields NjU0MDgA= ; I think you need to read up on Base64; it isn't just base64 values of ASCII characters. http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^] http://en.wikipedia.org/wiki/Base_64[^]

              N 1 Reply Last reply
              0
              • P PIEBALDconsult

                I'm not following you. From TmpVME1EZ0E9 I get 546D70564D4531455A304539 The sequence you state yields NjU0MDgA= ; I think you need to read up on Base64; it isn't just base64 values of ASCII characters. http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.110).aspx[^] http://en.wikipedia.org/wiki/Base_64[^]

                N Offline
                N Offline
                NJdotnetdev
                wrote on last edited by
                #7

                Thanks for your help. I was able to solve it. Following is what I ended up with: byte[] decoded = Convert.FromBase64String("UUEncoded string to be decoded"); String value = ASCIIEncoding.ASCII.GetString(decoded); int output = Convert.ToInt32(value); // Convert int to hex format and reverse the byte String.Format("{0:X02}{1:X02}{2:X02}", (output & 0x0000FF) >> 0, (output & 0x00FF00) >> 8, (output & 0xFF0000) >> 16);

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups