顯示包含「C」標籤的文章。顯示所有文章
顯示包含「C」標籤的文章。顯示所有文章

2023年2月14日星期二

C atol() atoll() 字串轉長整數

atol() – atoll() —Convert Character String to Long or Long Long Integer

IBM說明

The atol() function converts a character string to a long value. The atoll() function converts a character string to a long long value.

The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character can be the null character that ends the string.

我的理解:

將字串轉成長整數,分別會有 long long long 看使用在哪個平台上,需要特別注意並使用正確的變數。



(字串接受格式)

下列使用atol()為例,使用long 去接轉換後回傳值

程式碼

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *strnumber = " 1234567890";
    long number_out;

    number_out = atol(strnumber);
    printf("number_in:\"%s\"\t,number_out:%ld\n", strnumber, number_out);
}
執行結果


參考資料

https://www.ibm.com/docs/en/i/7.4?topic=lf-atol-atoll-convert-character-string-long-long-long-integer

C atof 字串轉浮點數

atof() — Convert Character String to Float

IBM說明

The atof() function converts a character string to a double-precision floating-point value.

The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character can be the null character that ends the string.

我的理解:

將字串轉成倍精度浮點數值型態,只要字串是有意義可被解讀的(一般的小數點表示與科學記號表示),並符合圖片的格式皆可轉換。

下列會以正的浮點數,負的浮點數,科學記號表示分別呈現,使用float型態儲存轉換結果


(字串接受格式)

程式碼

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *strnumber = " 123.345";
    float number_out;

    number_out = atof(strnumber);
    printf("Ex1.number_in:\"%s\"\t,number_out:%f\n", strnumber, number_out);

    strnumber = " 12.345E-5";
    number_out = atof(strnumber);
    printf("Ex2.number_in:\"%s\"\t,number_out:%e\n", strnumber, number_out);

    strnumber = "-123.345";
    number_out = atof(strnumber);
    printf("Ex3.number_in:\"%s\"\t,number_out:%f\n", strnumber, number_out);

    strnumber = "-12.345E-5";
    number_out = atof(strnumber);
    printf("Ex4.number_in:\"%s\"\t,number_out:%e\n", strnumber,  number_out);
}

執行結果


根據輸出結果,我們可以觀察到

Ex1. 字串123.345經過轉換再印出來的結果為123.345.001

Ex2. 字串12.345E-5經過轉換再印出來的結果為1.2345e-004

Ex3. 字串-123.345經過轉換再印出來的結果為123.345.001

Ex4. 字串-12.345E-5經過轉換再印出來的結果為-1.234500e-004

那為甚麼有些後面會冒出一個1?原因是浮點數的表示方式與整數不一樣,才會有此現象

參考資料

https://www.ibm.com/docs/en/i/7.4?topic=functions-atof-convert-character-string-float#itof

2023年2月11日星期六

C atoi 字串轉整數

 

atoi() — Calculate Integer Absolute Value

IBM 說明

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character can be the null character that ends the string.

我的理解:此函式會將輸入的整數取決對值後回傳,字串的格式如果是符合下圖的,基本上都可以轉換。

(字串接受格式)

程式碼

#include <string.h>
#include <stdio.h>

int main(void)
{
    char *strnumber = " 123";
    int number_out;

    number_out = atoi(strnumber);
    printf("number_in:%s,number_out:%d\n", strnumber, number_out);

    strnumber = "-321";
    number_out = atoi(strnumber);
    printf("number_in:%s,number_out:%d\n", strnumber, number_out);

}

執行結果


參考資料

https://www.ibm.com/docs/en/i/7.4?topic=functions-atoi-convert-character-string-integer


打賞按讚