Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pb.encode double/float 类型数据,在C++里ParseFromString会有问题。 #258

Open
hz-bin opened this issue Jan 4, 2024 · 2 comments
Labels

Comments

@hz-bin
Copy link

hz-bin commented Jan 4, 2024

lua代码:

protoc:load([[
	syntax = "proto3";
	message TestNumber {
		double a = 1;
		float b = 2;
		int32 c = 3;
	}
]])
local tt = {
	a = 100,
	b = 200,
	c = 300
}
local s = pb.encode("TestNumber", tt)
PBTestNumber(s)

C++代码:

int PBTestNumber(lua_State* L) {
    string s = lua_tostring(L, 1);
    TestNumber* msg = new TestNumber();
    msg->ParseFromString(s);
    std::cout << "in PBTestNumber msg=" << msg->ShortDebugString() << std::endl;
    return 0;
}

这里输出的是:in PBTestNumber msg=c: 300

@starwing starwing added the bug label Jan 6, 2024
@starwing
Copy link
Owner

starwing commented Jan 6, 2024

我感觉ParseFromString有问题,得到的是二进制数据,你没取长度会在0的地方截断

@starwing starwing added question and removed bug labels Jan 6, 2024
@hz-bin
Copy link
Author

hz-bin commented Jan 7, 2024

我感觉ParseFromString有问题,得到的是二进制数据,你没取长度会在0的地方截断

用ParseFromArray,改成下面这样就ok了。多谢大佬。

int PBTestNumber(lua_State* L) {
    size_t len;
    const char* s = lua_tolstring(L, 1, &len);
    TestNumber * msg = new TestNumber();
    msg->ParseFromArray(s, len);
    std::cout << "in PBTestNumber msg=" << msg->ShortDebugString() << std::endl;
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants