PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   疑難雜症區 (https://www.pcdvd.com.tw/forumdisplay.php?f=34)
-   -   c++ string to int (hex) (https://www.pcdvd.com.tw/showthread.php?t=952571)

Voodoo6000 2011-12-15 11:01 AM

c++ string to int (hex)
 
小弟求救一下
我c++是用 Dev-C++寫的

我想把string轉成int 16進位的

例如:

string str=FF

轉成int 16進位 a=FF

當然要對a做運算
+1 = 100
-1 = FE

請問何解qq”

疾如風 2011-12-16 11:53 AM

把16進位string轉成int偷懶的作法,用std::stringstream

代碼:
#include <sstream>
#include <iostream>

using namespace std;

int main() {
    int a;
    stringstream ss;
    string str = "FF";

    ss << hex << str;
    ss >> a;
    cout << hex << a+1 << " " << a-1 << endl;

    return 0;
}

PAN_PAN 2011-12-16 12:05 PM

引用:
作者Voodoo6000
小弟求救一下
我c++是用 Dev-C++寫的

我想把string轉成int 16進位的

例如:

string str=FF

轉成int 16進位 a=FF

當然要對a做運算
+1 = 100
-1 = FE

請問何解qq”


請問是來問作業的嗎? 如果是請把英文練好, 如果不是也把英文練好, 然後 Google, 我隨便 Google C++ string to hex 第一個出現的 link 就有教了 :rolleyes:

kenchen_1226 2011-12-16 12:08 PM

引用:
作者Voodoo6000
小弟求救一下
我c++是用 Dev-C++寫的

我想把string轉成int 16進位的

例如:

string str=FF

轉成int 16進位 a=FF

當然要對a做運算
+1 = 100
-1 = FE

請問何解qq”


想辦法把 string 轉成 char *
然後用 atoi() 轉換就可以了


例如這樣

char * token = str.c_str();
int number = atoi(token);

沒 compiler, 大概可以吧

Voodoo6000 2011-12-16 12:38 PM

恩 已經解決了

從xxx.txt讀取出來變成char txt [ ]資料型態
再用sscanf轉成16進位int型態
加減乘除完畢 在轉成char txt [ ] 型態 在寫回xxx.txt檔案 裡面

:stupefy:


所有的時間均為GMT +8。 現在的時間是07:15 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。