C++字符串轉數字並進行進制轉換——stoi

可以使用 cstring頭文件中的stoi函數實現:字符串轉數字,數字進行進制轉換

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a=stoi("12",0,16);
    //字符串 12 從第0位開始 轉換成16進制
    cout <<"十進制:12    轉換成 十六進制:" <<a<<endl;

    a=stoi("12",0,8);
    //字符串 12 從第0位開始 轉換成8進制
    cout <<"十進制:12    轉換成 八進制:" <<a<<endl;
    return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章