簡體   English   中英

C ++:將std :: string轉換為UINT64

[英]C++: Convert std::string to UINT64

我需要將從文本文件輸入的數字的(十進制,如果需要的話)字符串表示形式轉換為UINT64,以傳遞給我的數據對象。

    size_t startpos = num.find_first_not_of(" ");
    size_t endpos = num.find_last_not_of(" ");
    num = num.substr(startpos, endpos-startpos+1);
    UINT64 input;
    //convert num to input required here

有什么方法可以像atoi()一樣將std :: string轉換為UINT64嗎?

謝謝!

編輯:下面的工作代碼。

size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);

std::istringstream stream (num);
stream >> input;

使用strtoull_strtoui64() 例:

std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);

至少有兩種方法可以執行此操作:

  1. 構造一個std::istringstream ,並使用我們的老朋友>>運算符。

  2. 自己轉換即可。 一次解析所有數字,將它們轉換為單個整數。 這是一個很好的練習。 由於這是一個無符號值,因此甚至不必擔心負數。 我認為這將是任何計算機科學入門課的標准作業。 至少是在我的時代。

您可以使用stoull

char s[25] = "12345678901234567890"; // or: string s = "12345678901234567890";
uint64_t a = stoull(s);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM