Menu

Encode binary data in base64

Help
Anonymous
2003-03-21
2003-03-24
  • Anonymous

    Anonymous - 2003-03-21

    Hello,

    I am currently trying to send and receive binary data to the server: milk.orbiteam.de. Simple operations are no problem with your classes but now my problem is to encode the binary files in base64. In the class XmlRpcValue there seems to be code to do that. But cause of little documentation I am not able to use it. Can someone help me ? Is there any complex documentation of how to use the classes ?

    Cheers,
    Jochen

     
    • Anonymous

      Anonymous - 2003-03-21

      meanwhile I solved this problem myself.
      It was much easier than I thought.

      But now I have another problem. I made the first tests with the library easily by changing the existing example HelloClient. Now I want to make my own project. But how do I have to include the xmlrpc.lib in Microsoft Visual C++.NET ?
      If I include the .lib in the project properties the compiler always sends error messages because he couldn't find the namespace XmlRpc.
      But it is the same code I used before.

      What else do i have to change in the project properties ?

       
      • Chris Morley

        Chris Morley - 2003-03-21

        For project settings, be sure to match the run time library settings and enable runtime type info and exception handling. But those won't cause the errors you mention.

        If you are getting compiler errors about not recognizing the XmlRpc namespace, be sure you have included the appropriate header files. Usually

        #include <XmlRpc.h>

        will include everything that you need.
        In my examples, for brevity I write:

        using namespace XmlRpc;

        but in my production code I use the fully qualified names, eg:

        XmlRpc::XmlRpcValue args;

        Chris

         
    • Chris Morley

      Chris Morley - 2003-03-21

      I'm working on getting more documentation done...

      For the benefit of any one else reading this thread, to construct an XmlRpcValue from arbitrary binary data:

      void *myDataPtr = ... // A pointer to your data
      int nDataBytes = ...  // The number of bytes of data

      XmlRpcValue binDataValue(myDataPtr, nDataBytes);

      To access the binary data in an XmlRpcValue, you can access the std::vector<char> of bytes (there is a typedef for it called BinaryData):

      XmlRpcValue::BinaryData const& binDataChars = binDataValue;
      for (int i=0; i<binDataChars.size(); ++i)
      // do something with binDataChars[i] ...

      Chris

       
    • Chris Morley

      Chris Morley - 2003-03-21

      The part I forgot to mention above is that encoding to and decoding from base64 is only done when the value needs to be converted to/from xml; if you want to base64 encoding of the data you can use the toXml() method or write() the value to a strstream.

      Chris

       
      • Anonymous

        Anonymous - 2003-03-24

        and what if I want to decode the data from base64 ?
        I have downloaded a xml-file from the server and now need to parse it. Therefor I need the plain xml-text again. How can I do that?
        I tried something like:
        std::string plaintext (result);
        but that does not work.

         
        • Chris Morley

          Chris Morley - 2003-03-24

          > and what if I want to decode the data from base64 ?

          The library does the encoding and decoding of data to and from base64; normally you won't ever see it as base64. You will just see your data.

          The most efficient way to access binary data in an XmlRpcValue is to use the XmlRpcValue::operator BinaryData&() to access the std::vector<char> containing the data. See the file test/TestBase64Client.cpp for an example of a program that receives a result containing binary data, accesses the vector containing the data, and then dumps the data to a file.

          If you run the TestBase64Server and Client, you should see that the file written by the client is identical to the one sent by the server.

          In your situation, it sounds like rather than dumping the data to a file you want to create a string, so you might do something like this:

          // call the RPC method
          XmlRpcValue noArgs, result;
          c.execute("GetXmlAsBinaryData", noArgs, result);
          // access the binary data in the result
          const XmlRpcValue::BinaryData& data = result;
          // construct a string from the data
          std::string resultString(&data[0],data.size());

          Note that the last line of my example is not strictly C++ standard conforming, because it assumes the vector storage referenced via &data[0] is a single contiguous chunk, so modify as you see fit...

           
    • Anonymous

      Anonymous - 2003-03-24

      Thanks for your efforts. Finally my program is working fine. And thanks a lot for this useful library !!

       

Log in to post a comment.