整型數轉成字元,以及itoa,atoi

2021-05-26 02:23:35 字數 858 閱讀 7466

(1)將整型數12345轉單字元輸出,需強記幾個字元的ascii碼:null的是0;0的是0x30;a的是0x41;a的是0x61。程式如下:

int main(int argc, char* argv)

;array[0] = (unsigned char)(num/10000);//型別要匹配,逐漸分離出高位,所以要求餘

temp = num%10000;

array[1] = (unsigned char)(temp/1000);

temp = temp%1000;

array[2] = (unsigned char)(temp/100);

temp = temp%100;

array[3] = (unsigned char)(temp/10);

array[4] = (unsigned char)(temp%10);

for(int i =0;i<5;i++)

printf("%c/n",0x30+array[i]);

return 0;

}(2)庫函式,linux中無此函式

字串轉整型的庫函式:atoi,原型:int atoi(const char *nptr),標頭檔案#include,把nptr所指向的字串轉成整型數。

輸出結果是12345.67,12345。第二個直接將對應字串轉換成整型數輸出,注意已經拋棄小數字。

整型數轉字串的庫函式:itoa,原型:char *itoa(int value, char *string, int radix),將整型數value轉成字串以string返回,radix是轉換的基數,比如十進位制,八進位制。

輸出是3455,3455。可善用這兩個函式用來在整型數和字串引數間轉換輸出需求。

將整型字串轉成整數值

題目 給定乙個字串str,如果str符合日常書寫的整數形式,並且屬於32位整數的範圍,返回str所代表的整數值,否則返回0。舉例 str 123 返回123。str 023 因為不符合日常的書寫習慣,返回0。str a123 返回0。str 2147483647 返回2147482647 str 2...

ascii碼字串轉成整型陣列

for int buffer len 1 buffer len 0x41 else if buffer buffer len 0x66 buffer buffer len 0x61 else if buffer buffer len 0x41 buffer buffer len 0x46 else ...

字元陣列 字串 整型數之間的轉化

1 字元陣列 轉化為 字串 應用字串定義時的建構函式 include using namespace std 字元陣列轉化為字串 include include int main 2 字串 轉化為 字元陣列 應用strncpy函式 include using namespace std 字串轉化為字...