是否有將字符串轉換爲無符號整型的方法? _ultoa存在,但找不到vise詩句版本...如何將ascii轉換爲無符號整數
回答
std::strtoul()
是一個。然後再次有像atoi()
這樣的舊的。
Boost提供lexical_cast。
#include <boost/lexical_cast.hpp>
[...]
unsigned int x = boost::lexical_cast<unsigned int>(strVal);
或者,你可以使用一個字符串流(這基本上是lexical_cast的做什麼在幕後):
#include <sstream>
[...]
std::stringstream s(strVal);
unsigned int x;
s >> x;
如果您想要非小數解釋,請參閱:http://stackoverflow.com/questions/1070497/c-convert- hex-string-to-signed-integer – Martin 2009-09-30 07:23:00
我是唯一喜歡流式插入但喜歡流式抽取的人嗎?我會每次都使用函數(比如這個提升函數,儘管說實話我可能還會用atoi而不用考慮)。 – Steve314 2009-09-30 07:25:11
是的,流提取是非常醜陋的,特別是因爲你不能用它初始化常量。但它也有點強大,因爲你可以使用操縱器來改變你的基礎,等等。 – Martin 2009-09-30 07:27:20
如何int atoi (const char * str)?
string s("123");
unsigned u = (unsigned)atoi(s.c_str());
這根本不是一個好的建議。他被特別要求提供「unsigned int」,並提及諸如「_ultoa」之類的東西。海報知道atoi(),atol()等等。這些函數都是經過簽名和識別的 - 符號,可能會處理溢出等。正確的答案是使用像strtoul這樣的函數。 – Armentage 2011-04-19 04:31:34
sscanf會做你想做的。
char* myString = "123"; // Declare a string (c-style)
unsigned int myNumber; // a number, where the answer will go.
sscanf(myString, "%u", &myNumber); // Parse the String into the Number
printf("The number I got was %u\n", myNumber); // Show the number, hopefully 123
如果通過_atoi64
無符號長L = _atoi64(STR)去它的工作原理;
- 1. TASM將乘法結果作爲ASCII符號輸出,如何轉換爲整數
- 2. 如何將_m128i轉換爲帶有SSE的無符號整數?
- 3. 將n位整數從無符號轉換爲有符號
- 4. 將無符號整數變量轉換爲帶符號變量
- 5. 將無符號整數轉換回char *?
- 6. 將無符號字符(數組)轉換爲無符號整數(數組)
- 7. 轉換爲unsigned char無符號整數*
- 8. 將無符號字符數組轉換爲整數
- 9. 將Ruby符號轉換爲整數
- 10. AWK將大無符號整數轉換爲有符號整數(二進制)
- 11. 如何將無符號轉換爲uint64_t?
- 12. 我如何十六進制的ASCII字符串轉換爲有符號整數
- 13. 從無符號長整型轉換爲無符號整型
- 14. 將負數浮點數轉換爲無符號整數
- 15. 將有符號整數轉換爲無符號長整型的最佳方法?
- 16. Xtext,將ascii字符轉換爲數學符號
- 17. Bash:將非ASCII字符轉換爲ASCII
- 18. 將無符號整數數組快速轉換爲base64
- 19. 以數學方式將簽名轉換爲無符號整數
- 20. SQL創建函數將字符串轉換爲ASCII整數
- 21. 如何將整數轉換爲字符
- 22. 無法將Int數組轉換爲ASCII
- 23. C - 如何將無符號整數秒轉換爲日期字符串
- 24. 如何將字符串時間轉換爲無符號整數在c + +
- 25. x86將ASCII字符轉換爲數字
- 26. 將數值轉換爲ASCII字符?
- 27. 將16位整數無符號數字轉換爲表示十六進制數字的ASCII字符串
- 28. 如何將uint32轉換爲無符號字符數組
- 29. 將MFC CString轉換爲無符號整數
- 30. 無法使用select_tag將符號轉換爲整數
atoi不會做無符號,但?在Windows CRT中,如果發生溢出,它將返回一個錯誤(ERANGE)。 – Cheeso 2010-03-25 02:43:09
Cheeso,對於我可以告訴的「我」來說,它是int ;-) – 2010-03-30 14:30:08