回答
如果我理解正確你的問題:
#include <stdio.h>
int main()
{
int i = atoi(" 3 ");
printf("%d", i);
return 0;
}
atoi
將忽略任何空白,直到它找到一個數字,當它檢測到一個非數字字符就會停止。
的評論說,幾乎一切,給你詳細介紹有關atoi
,sscanf
和strtol
。這裏我有一些示例字符串和這些函數返回的結果。因爲不是每個字符串都轉換成功我添加了一個擴展實現strtol
下面這個。生成此輸出的代碼也在下面給出see it online in action at ideone。
的atoi:字符串與非編號開始進行評估,以
0
。根本沒有錯誤信息。0: atoi with "3" to integer: +3 1: atoi with " 3 " to integer: +3 2: atoi with " -3 " to integer: -3 3: atoi with "str 3 " to integer: +0 4: atoi with "str-3 " to integer: +0 5: atoi with " 3str" to integer: +3 6: atoi with " -3str" to integer: -3 7: atoi with "str-3str" to integer: +0 8: atoi with "s-r-3str" to integer: +0
sscanf的:與
%d
就叫:同樣在這裏,但返回匹配的項目作爲計數,所以0
將指示錯誤。0: sscanf with "3" to integer: +3 | itemCount = 1 1: sscanf with " 3 " to integer: +3 | itemCount = 1 2: sscanf with " -3 " to integer: -3 | itemCount = 1 3: sscanf with "str 3 " to integer: +0 | itemCount = 0 --> Error! 4: sscanf with "str-3 " to integer: +0 | itemCount = 0 --> Error! 5: sscanf with " 3str" to integer: +3 | itemCount = 1 6: sscanf with " -3str" to integer: -3 | itemCount = 1 7: sscanf with "str-3str" to integer: +0 | itemCount = 0 --> Error! 8: sscanf with "s-r-3str" to integer: +0 | itemCount = 0 --> Error!
與strtol:同樣在這裏,但返回錯誤代碼爲
errno
的範圍溢出。另外endPtr
是爲您提供有關轉換的更多詳細信息的參數之一。0: strtol with "3" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a140, EndPtr = 0x7ffc47e9a141, PtrDiff = 1 1: strtol with " 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a130, EndPtr = 0x7ffc47e9a135, PtrDiff = 5 2: strtol with " -3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a120, EndPtr = 0x7ffc47e9a125, PtrDiff = 5 3: strtol with "str 3 " to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a110, EndPtr = 0x7ffc47e9a110, PtrDiff = 0 --> Error! 4: strtol with "str-3 " to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a100, EndPtr = 0x7ffc47e9a100, PtrDiff = 0 --> Error! 5: strtol with " 3str" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a0f0, EndPtr = 0x7ffc47e9a0f5, PtrDiff = 5 6: strtol with " -3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0e0, EndPtr = 0x7ffc47e9a0e5, PtrDiff = 5 7: strtol with "str-3str" to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a0d0, EndPtr = 0x7ffc47e9a0d0, PtrDiff = 0 --> Error! 8: strtol with "s-r-3str" to integer: +0 | errno = 0, StartPtr = 0x7ffc47e9a0c0, EndPtr = 0x7ffc47e9a0c0, PtrDiff = 0 --> Error!
這裏是我的strtol
函數的代碼,這樣就可以解析更多不同的字符串。 my_strtol
的用法與strtol
完全相同。如果你有如下字符串,還有一個幫助函數:"str-str-3"
,因爲第一個減號-
不是整數。
my_strtol()實現:
/* Position exactly to integer beginning */
/* e.g. if you have: "str-str-3str " */
/* the returning pointer will point to: */
/* "-3str". */
const char* getNumberPos(const char* str)
{
const char* curChar = str;
/* Iterate through part of integer is there. */
while ( '\0' != curChar
&& !(isdigit(*curChar) || '+' == *curChar || '-' == *curChar))
{
++curChar;
}
/* Check if the sign is part of an integer, */
/* if not use this function recursive! */
if ('+' == *curChar || '-' == *curChar)
{
const char* nextChar = curChar + 1;
if ( '\0' != *nextChar
&& !isdigit(*nextChar))
{
++curChar;
curChar = getNumberPos(curChar);
}
}
return curChar;
}
/* Extended strtol function */
long my_strtol(const char* str, char** endptr, int base)
{
const char* curChar = getNumberPos(str);
return strtol(curChar, endptr, base);
}
my_strtol的結果:的my_strtol
功能與上面給出的所有字符串的工作原理:
0: my_strtol with "3" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a140, EndPtr = 0x7ffc47e9a141, PtrDiff = 1
1: my_strtol with " 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a130, EndPtr = 0x7ffc47e9a135, PtrDiff = 5
2: my_strtol with " -3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a120, EndPtr = 0x7ffc47e9a125, PtrDiff = 5
3: my_strtol with "str 3 " to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a110, EndPtr = 0x7ffc47e9a115, PtrDiff = 5
4: my_strtol with "str-3 " to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a100, EndPtr = 0x7ffc47e9a105, PtrDiff = 5
5: my_strtol with " 3str" to integer: +3 | errno = 0, StartPtr = 0x7ffc47e9a0f0, EndPtr = 0x7ffc47e9a0f5, PtrDiff = 5
6: my_strtol with " -3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0e0, EndPtr = 0x7ffc47e9a0e5, PtrDiff = 5
7: my_strtol with "str-3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0d0, EndPtr = 0x7ffc47e9a0d5, PtrDiff = 5
8: my_strtol with "s-r-3str" to integer: -3 | errno = 0, StartPtr = 0x7ffc47e9a0c0, EndPtr = 0x7ffc47e9a0c5, PtrDiff = 5
下面的代碼顯示的是測試代碼爲所有四個功能生成輸出。
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
int main()
{
/* Loop index */
int idx;
/* Declare and initialize char arrays. */
char array[] = { "3" };
char arrayI[] = { " 3 " };
char arrayII[] = { " -3 " };
char arrayIII[] = { "str 3 " };
char arrayIV[] = { "str-3 " };
char arrayV[] = { " 3str" };
char arrayVI[] = { " -3str" };
char arrayVII[] = { "str-3str" };
char arrayVIII[] = { "s-r-3str" };
/* Save each array in one array for iteration purposes. */
const char* const arrays[] = { array,
arrayI,
arrayII,
arrayIII,
arrayIV,
arrayV,
arrayVI,
arrayVII,
arrayVIII };
/* Get size of that array. */
const int arraysSize = sizeof(arrays)/sizeof(char*);
/* atoi(): Does not detect errors! */
for (idx = 0; idx < arraysSize; ++idx)
{
int integer = atoi(arrays[idx]);
printf("%d: atoi with \"%s\" %*s to integer: %+d\n",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer);
}
printf("\n");
/* sscanf(): Error detection by using the matched item count. */
for (idx = 0; idx < arraysSize; ++idx)
{
int integer = 0;
int itemCount = sscanf(arrays[idx], "%d", &integer);
printf("%d: sscanf with \"%s\" %*s to integer: %+d | itemCount = %d",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
itemCount);
if (0 == itemCount)
{
printf(" --> Error!");
}
printf("\n");
}
printf("\n");
/* strtol(): Error detection by using errno and returned end pointer. */
for (idx = 0; idx < arraysSize; ++idx)
{
char* endPtr = NULL;
ptrdiff_t ptrDiff = 0;
errno = 0;
/* You have to check if the long can be stored in the int, not done here! */
int integer = (int) strtol(arrays[idx], &endPtr, 10);
ptrDiff = endPtr - arrays[idx];
printf("%d: strtol with \"%s\" %*s to integer: %+d | errno = %d, StartPtr = %p, EndPtr = %p, PtrDiff = %ld",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
errno,
(void*) arrays[idx],
(void*) endPtr,
ptrDiff);
if (0 != errno || 0 == ptrDiff)
{
printf(" --> Error!");
}
printf("\n");
}
printf("\n");
/* my_strtol: Same as strtol here. */
for (idx = 0; idx < arraysSize; ++idx)
{
char* endPtr = NULL;
ptrdiff_t ptrDiff = 0;
errno = 0;
/* You have to check if the long can be stored in the int, not done here! */
int integer = (int) my_strtol(arrays[idx], &endPtr, 10);
ptrDiff = endPtr - arrays[idx];
printf("%d: my_strtol with \"%s\" %*s to integer: %+d | errno = %d, StartPtr = %p, EndPtr = %p, PtrDiff = %ld",
idx,
arrays[idx],
8 - strlen (arrays[idx]),
"",
integer,
errno,
(void*) arrays[idx],
(void*) endPtr,
ptrDiff);
if (0 != errno || 0 == ptrDiff)
{
printf(" --> Error!");
}
printf("\n");
}
return 0;
}
嗨@AgrimTyagi如果這或任何答案已解決您的問題,請考慮[接受它](https://meta.stackexchange.com/q/5234/179419)點擊複選標記。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –
- 1. 如何使用atoi()將char數組的元素轉換爲int?
- 2. 如何將數組元素轉換爲Python中的整數
- 3. 如何將char [HEX]數組轉換爲整數?
- 4. 如何將char數組轉換爲整數或雙精度?
- 5. 如何連接char數組元素並將其轉換/轉換爲float?
- 6. 如何將IplImage轉換爲char數組
- 7. 如何將char數組轉換爲uintmax_t?
- 8. 如何將char數組轉換爲int?
- 9. 將TCHAR數組轉換爲char數組
- 10. 將char數組轉換爲int數組?
- 11. 將數組從unsigned char *轉換爲char *
- 12. 如何將可變數量的char數組元素轉換爲字符串
- 13. 我如何將這個元組元組轉換爲元素數?
- 14. 如何整數轉換成等於整數元素的數組
- 15. 將整數數組轉換爲char數組,每個整數每3位數
- 16. 如何將數組元素轉換爲PHP中的simplae元素?
- 17. 在JavaScript中將多維數組元素轉換爲整數
- 18. 將元組轉換爲整數
- 19. 將char轉換爲整數表示c#
- 20. 如何將數組轉換爲元組?
- 21. 當將字符串數組轉換爲整數數組時,元素變爲0
- 22. 將XML元素轉換爲數組
- 23. 如何將char數組轉換爲int數組?
- 24. 如何將char數組轉換爲字符串數組?
- 25. 如何將char數組轉換爲wchar_t數組?
- 26. 如何將2d char數組轉換爲2d int數組?
- 27. 如何將char數組轉換爲結構數組?
- 28. 如何將char數組元素轉換爲int和c中的其他方式?
- 29. 將整數轉換爲數組數組
- 30. 將16位整數轉換爲char數組? (C++)
[轉換字符數組到在C INT數]可能的複製(https://stackoverflow.com/questions/10204471/convert-char-array-to-a-int-number-in-c ) – Stargateur
我從字面上將你的標題問題複製並粘貼到谷歌中。 – Stargateur
它是數組內的*字符串*嗎?像「30」一樣? –