Convert string to __uint128_t using stringstreams
Asked Answered
I

1

3

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:

Is there any way to properly store my id_client in this __uint128_t variable ?

Inerrant answered 2/11, 2014 at 14:57 Comment(3)
As the type is not a standard type, the standard library have no support for it. You have to parse and convert the number manually.Recrystallize
Maybe you can use uint128_t if it is supported by your compiler.Silici
@JoachimPileborg I'm pretty sure he is looking for a complement to the standard library from the clang/gcc team as they have added the type into their compilers. It's an interesting question if it can be found.Patrilineage
C
1

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.

Cundiff answered 2/11, 2014 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.