use ByteArray

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

bqlf1907
Posts: 25
Joined: 13 May 2013, 10:28

use ByteArray

Postby bqlf1907 » 27 May 2013, 14:39

[OC]

Code: Select all

        NSData* vbyte =  [obj getByteArray:@"videobyte"];
        NSString *m = [[[NSString alloc] initWithData:vbyte encoding:NSUTF8StringEncoding] autorelease];
        string str = [m UTF8String];


[C++]

Code: Select all

      ByteArray* pByteArray = obj->GetByteArray(&string("videobyte"));
      string str;
      pByteArray->ReadUTF(str);


C++ it is right???
printf str is error(messy code)
How to write in C++??
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: use ByteArray

Postby MBagnati » 28 May 2013, 08:30

Your example sounds good.
I suggest you to check if GetByteArray has returned a valid object.

To verify the write/read to/from a ByteArray, you can also try this simple example where for each kind of data (bool, int, long, ...) I write a value into ByteArray and I get it back.

Code: Select all

     ByteArray* pByteArray;

     // ---------------------------------------------
     // Bool
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteBool(true);

     bool readBool;
     pByteArray->ReadBool(readBool);

     // ---------------------------------------------
     // Byte
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteByte(12);

     unsigned char readByte;
     pByteArray->ReadByte(readByte);

     // ---------------------------------------------
     // Double
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteDouble(123.456);

     double readDouble;
      pByteArray->ReadDouble(readDouble);

     // ---------------------------------------------
     // Float
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteFloat(456.789);

     float readFloat;
      pByteArray->ReadFloat(readFloat);

     // ---------------------------------------------
     // Int
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteInt(112233);

     long int readInt;
      pByteArray->ReadInt(readInt);

     // ---------------------------------------------
     // Long
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteLong(0x1122334455667788);

     long long readLong;
      pByteArray->ReadLong(readLong);

     // ---------------------------------------------
     // Short
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteShort(0x5566);

     short int readShort;
      pByteArray->ReadShort(readShort);

     // ---------------------------------------------
     // String
     // ---------------------------------------------

     pByteArray = new ByteArray();
     pByteArray->WriteUTF(new string ("This is a string"));

     string readString;
      pByteArray->ReadUTF(readString);


bqlf1907
Posts: 25
Joined: 13 May 2013, 10:28

Re: use ByteArray

Postby bqlf1907 » 28 May 2013, 08:48

oc and c++ connect the same server.
the data by oc client is normal;
the data by c++ client is error;
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: use ByteArray

Postby MBagnati » 28 May 2013, 10:16

Ok, can you please give me additional information to reproduce a similar scenario on my computer so that I can investigate the problem?
What is your software doing? Is it doing a request that returns a ByteArray created by server side?
Can you provide me an example?
bqlf1907
Posts: 25
Joined: 13 May 2013, 10:28

Re: use ByteArray

Postby bqlf1907 » 29 May 2013, 14:10

[C++]

Code: Select all

      ByteArray* pByteArray = obj->GetByteArray(&string("videobyte"));
      vector<unsigned char> vByteData;
      int size = pByteArray->Length();
      pByteArray->ReadBytes(size,vByteData);
      string str(vByteData.begin(),vByteData.end());


Problem has been resolved。
ths str is normal.
MBagnati
Posts: 126
Joined: 12 Feb 2013, 10:57

Re: use ByteArray

Postby MBagnati » 11 Jun 2013, 09:05

ok, thanks

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 35 guests