问题描述
我正在尝试从字符串中提取不同类型的数据.
I'm trying to extract different types of data from a string.
void readHeader(char buf[BUFFSIZE])
{
std::istringstream hdr(buf);
__uint128_t id_client;
hdr >> id_client; // doesn't compile
}
执行此操作时出现此错误:
I'm getting this error when I do that hdr >> id_client
:
Unix/UnixSocket.cpp:158:10: error: ambiguous overload for ‘operator>>’ in ‘hdr >> id_client’ Unix/UnixSocket.cpp:158:10: note: candidates are: In file included from /usr/include/c++/4.7/sstream:39:0,
from Unix/UnixSocket.cpp:11: /usr/include/c++/4.7/istream:118:7: note: std::basic_istream<_CharT,
_Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT,
_Traits>::__istream_type = std::basic_istream<char>] <near match> /usr/include/c++/4.7/istream:118:7: note: no known conversion for argument 1 from ‘__int128 unsigned’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’ /usr/include/c++/4.7/istream:122:7: note: std::basic_istream<_CharT,
_Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT,
_Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match> /usr/include/c++/4.7/istream:122:7:
有什么办法可以将我的id_client
正确存储在此__uint128_t
变量中?
Is there any way to properly store my id_client
in this __uint128_t
variable ?
推荐答案
https://gmplib.org/可能帮助. gmpxx对象抽象的mpz_class类支持I/O运算符,并且mpz_export(...)函数允许您将结果转换为字节数组.如果它们超过16个字节,您可能会引发异常或以其他方式抱怨.速度不是很快,但我想实现起来很快.
https://gmplib.org/ might help. The mpz_class class of the gmpxx object abstraction supports I/O operators and the mpz_export(...) function allows you to transform the result into an array of bytes. If they exceed 16 bytes you may throw an exception or complain otherwise. Not very fast but I guess fast to implement.
这篇关于使用stringstreams将字符串转换为__uint128_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!