How to convert "string" into "byte array" and vice versa?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yabansu
    New Member
    • Dec 2006
    • 14

    How to convert "string" into "byte array" and vice versa?

    Hi all,

    I think most of you probably know the two .NET framework functions, namely Encoding.GetByt es(string) and Encoding.GetStr ing(byte[]), to convert string into byte array and vice versa.

    Now, I want to do the same thing in pure(unmanaged) C++.

    I searched the Internet but could not find any satisfactory solutions. I am really in need of help! Is there anyone to explain how I can implement these functions by not using .NET library?

    Thanks a lot...
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. In c++, BYTE is an unsigned char and a string is a char array. Could you let us know where you want to start and where you want to end and why?

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      if you are using C++ strings the c_str() function will return a const pointer to an array of char where each char is a byte?
      http://www.cppreferenc e.com/cppstring/index.html

      Comment

      • yabansu
        New Member
        • Dec 2006
        • 14

        #4
        Originally posted by willakawill
        Hi. In c++, BYTE is an unsigned char and a string is a char array. Could you let us know where you want to start and where you want to end and why?
        Hi willakawill,
        I am working on a basic client/server application. Here, it is supposed to encrypt some data and send the encrypted data to each other in xml packets.
        The encryption algorithm I used, takes byte array as argument and gives byte array as output. Since the resultant byte array cannot be directly inserted into the xml packet, I need to convert it into a string. Also, the other side needs to convert it into the byte array to use after parsing the xml packet.
        I hope the problem is clear.

        Comment

        • yabansu
          New Member
          • Dec 2006
          • 14

          #5
          To make more clear, I need the following two functions:

          1. a function takes a string parameter and returns the unsigned char array representation of the string
          2. a function takes a unsigned char array parameter and returns the string representation of the array

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by yabansu
            To make more clear, I need the following two functions:

            1. a function takes a string parameter and returns the unsigned char array representation of the string
            2. a function takes a unsigned char array parameter and returns the string representation of the array
            if you usethe C++ string class
            http://www.cppreferenc e.com/cppstring/index.html
            the c_str() function will do (1) and there is a constructor to do 2.

            Comment

            • Daggerbot
              New Member
              • Jan 2011
              • 1

              #7
              If you're using std::string, that won't be necessary, because a C++ string is already a byte array. Use string.length() to get the number of bytes, and string.data() to get a pointer to the first byte. But it really depends on what you plan to do with those bytes.

              Comment

              Working...