最近在linux下用到uint64_t類型,需要存儲長達20位的ID號,需要用到類型轉換。
從字符串到uint64_t轉換,想到了atoi,atol,atoll,strtol,結果發現18446744073709551616全被截斷了。
(unsigned long long)atoll(18446744073709551616); 這個肯定是不行的。
後面用到了stringstream 才把問題解決,包含頭文件<sstream>
char *p = "18446744073709551616";
stringstream strValue;
strValue << p;
uint64_t value;
strValue >> value;
這樣可以字符串轉換爲uint64_t類型了。
stringstream 除了類型轉換外,功能很強大,具體查閱相關資料。