使用谷歌协议缓冲区传输字符串字符时,得到了凌乱的代码 [英] When using google protocol buffers to transfer String character,got messy code
本文介绍了使用谷歌协议缓冲区传输字符串字符时,得到了凌乱的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在调试视图中:
这是编码为凌乱字符串的代码...
Here is the code which encodes into messy string...
((S2CEnterCollection)objS2c).toByteString().toStringUtf8();
输出:
���"default(
���"default(
���"default(
���"default(
���"default(
����"default(
����"default(
�����"default(
以下是具有正确字符串的代码:
Here is the code which has the right string:
((S2CEnterCollection)objS2c).toString()
原始字符串为:
cardList {
cardId: 100001
liked: 100
number: 10
finder: "default"
rank: 1
}
cardList {
cardId: 100002
liked: 123
number: 10
finder: "default"
rank: 1
}
cardList {
cardId: 100003
liked: 543
number: 10
finder: "default"
rank: 1
}
cardList {
cardId: 100004
liked: 766
number: 10
finder: "default"
rank: 1
}
cardList {
cardId: 100005
liked: 78
number: 10
finder: "default"
rank: 1
}
cardList {
cardId: 100006
liked: 89
number: 123
finder: "default"
rank: 1
}
cardList {
cardId: 100007
liked: 199
number: 567
finder: "default"
rank: 1
}
cardList {
cardId: 100008
liked: 90909
number: 232
finder: "default"
rank: 1
}
那么,有没有人知道它是如何工作的?
So, does anyone know how it works?
推荐答案
protobuf数据是二进制的,是没有编码的文字。你不能通过像UTF-8这样的编码来运行它,并期望得到一个字符串(或者期望它仍然有效)。将protobuf数据转换为字符串的唯一方法是通过base-N编码运行一些N,通常为64(因为它在大多数平台上得到很好的支持)。
protobuf data is binary, and is not encoded text. You cannot run it through an encoding like UTF-8 and expect to get a string (or expect it to still be valid). The only way to convert protobuf data to a string is to run it through a base-N encode for some N, typically 64 (because it is well-supported on most platforms).
这篇关于使用谷歌协议缓冲区传输字符串字符时,得到了凌乱的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文