服務器返回protobuf的數據在c++/lua中傳輸保持完整

針對問題:對於服務器返回的數據中存在‘\0’,從c++導入到lua中存在數據遺失問題

運行環境:cocos2dx-3.1.1,vs2013,protobuf2.6.1

參考資料:

http://www.himigame.com/lua-game/1644.html

http://blog.csdn.net/tianxiawuzhei/article/details/43022303



一、搭建protobuf環境。參考(http://blog.csdn.net/daydayup_chf/article/details/49904785)

二、解決問題。

在c++得到服務器返回的protobuf數據後,傳給lua時,使用  lua_pushlstring(tolua_S, ret, 18);  方法

例子:


char* ret = cobj->GetProtoData((int8_t*)arg0, arg1);  //GetProtoData是得到服務器返回數據的方法(方法中參數請無視), ret是要得到的數據。

lua_pushlstring(tolua_S, ret, a);  //a是ret的數據長度。這裏注意不要使用strlen方法,因爲這樣得到的長度是錯的。理由是在ret中任意地點存在結束符

發佈了39 篇原創文章 · 獲贊 15 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章