C/C++ 數字轉字串的方法
C/C++ 數字轉字串的方法
資料來源: https://www.delftstack.com/zh-tw/howto/c/how-to-convert-an-integer-to-a-string-in-c/
https://www.tutorialspoint.com/compile_c_online.php
CodeBlock 12.11
01.sprintf() 函式在 C 語言中把一個整數轉換成字串
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
char text[20];
number=0xFF;
sprintf(text, "%d", number);
printf("\nYou have entered: %s", text);
sprintf(text, "%o", number);
printf("\nYou have entered: %s", text);
sprintf(text, "%x", number);
printf("\nYou have entered: %s", text);
return 0;
}
02.itoa() 函式在 C 語言中把整數轉換為字串 [ itoa() 不是一個 ANSI C 標準函式。根據你的平臺,它可能無法在你的 GCC 編譯器上工作。]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int number;
char string[20];
number=0xff;
itoa(number,string,10);
printf("String value = %s\n", string);
itoa(number,string,8);
printf("String value = %s\n", string);
itoa(number,string,16);
printf("String value = %s\n", string);
return 0;
}
2 thoughts on “C/C++ 數字轉字串的方法”
C/C++ 數字轉字串的方法
十進位/八進位/十六進位
十進制/八進制/十六進制
10進位/8進位/16進位
10進制/8進制/16進制
C/C++ 字串轉數字的方法
https://bit.ly/3vzkt2h