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. String to Byte Array Conversion

String to Byte Array Conversion

Scheduled Pinned Locked Moved C#
data-structureshelp
7 Posts 4 Posters 1 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.
  • M Offline
    M Offline
    M J Jaya Chitra
    wrote on last edited by
    #1

    I want to convert a string to byte array as follows byte[] b = Convert.FromBase64String(s); it is giving the following error: Invalid length for a Base-64 char array. can any one give me a suggestion to solve this.

    Best Regards, M. J. Jaya Chitra

    K S 2 Replies Last reply
    0
    • M M J Jaya Chitra

      I want to convert a string to byte array as follows byte[] b = Convert.FromBase64String(s); it is giving the following error: Invalid length for a Base-64 char array. can any one give me a suggestion to solve this.

      Best Regards, M. J. Jaya Chitra

      K Offline
      K Offline
      kristmun
      wrote on last edited by
      #2

      I guess you could use the Encoding class (System.Text). Encoding encoding = Encoding.UTF8; byte[] b = encoding.GetBytes(s); But you have to use the right encoding. Hope this helps.

      1 Reply Last reply
      0
      • M M J Jaya Chitra

        I want to convert a string to byte array as follows byte[] b = Convert.FromBase64String(s); it is giving the following error: Invalid length for a Base-64 char array. can any one give me a suggestion to solve this.

        Best Regards, M. J. Jaya Chitra

        S Offline
        S Offline
        Sathesh Sakthivel
        wrote on last edited by
        #3

        For Conversion of String to Byte Array go like this, public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); } Don't go with Base 64 because it is of invalid length. Because of an unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

        Regards, Satips.:rose:

        M G 2 Replies Last reply
        0
        • S Sathesh Sakthivel

          For Conversion of String to Byte Array go like this, public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); } Don't go with Base 64 because it is of invalid length. Because of an unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

          Regards, Satips.:rose:

          M Offline
          M Offline
          M J Jaya Chitra
          wrote on last edited by
          #4

          Thank you, Thanks a lot.

          Best Regards, M. J. Jaya Chitra

          S 1 Reply Last reply
          0
          • S Sathesh Sakthivel

            For Conversion of String to Byte Array go like this, public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); } Don't go with Base 64 because it is of invalid length. Because of an unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

            Regards, Satips.:rose:

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Satips wrote:

            For Conversion of String to Byte Array go like this, public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); }

            Or simply: byte[] data = System.Text.Encoding.ASCII.GetBytes(str);

            --- single minded; short sighted; long gone;

            S 1 Reply Last reply
            0
            • M M J Jaya Chitra

              Thank you, Thanks a lot.

              Best Regards, M. J. Jaya Chitra

              S Offline
              S Offline
              Sathesh Sakthivel
              wrote on last edited by
              #6

              Welcome :)

              Regards, Satips.:rose:

              1 Reply Last reply
              0
              • G Guffa

                Satips wrote:

                For Conversion of String to Byte Array go like this, public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); }

                Or simply: byte[] data = System.Text.Encoding.ASCII.GetBytes(str);

                --- single minded; short sighted; long gone;

                S Offline
                S Offline
                Sathesh Sakthivel
                wrote on last edited by
                #7

                Yes Guffa. But if i give it Like that Jaya will ask how ? so i typed it as so simple format so that she can easily understand the concept.

                Regards, Satips.:rose:

                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