2011-05-26 48 views
1

嘿大家,我正在寫一個軟件,由於我選擇使用Qt,需要我重寫幾百行代碼才能符合unicode(qt強制執行我的整個項目_UNICODE)。在寫完所有這些代碼並使其工作之後,我幾乎完全自殺了,但遇到了_wtoi()的問題 - 它不是跨平臺的!跨平臺_wtoi()實現?

任何人都可以幫我快速實施?我聽說過使用stringstreams來做它,並保持跨平臺,但我從來沒有使用它們,並將它用作unicode atoi()似乎暗示了我。我更喜歡有人向我解釋它,而不是僅僅通過stringstreams例子從現成的網站和論壇複製代碼。

謝謝!

+4

爲什麼不使用wcstol()? – 2011-05-27 00:20:31

+0

我聽說它不像其他解決方案那樣安全。然而,我可能是錯的。 – 2011-05-27 00:27:48

+1

你是。它實際上可以讓你檢查是否有一個數字,_wtoi()只返回0。 – 2011-05-27 00:35:02

回答

1

如果你想純粹的運行時間速度,我建議BoostSpiritQi

#include <cwchar> 
#include <boost/spirit/include/qi_int.hpp> 
#include <boost/spirit/include/qi_parse.hpp> 

int portable_wtoi(wchar_t const* const str) 
{ 
    using boost::spirit::int_; 
    using boost::spirit::qi::parse; 

    wchar_t const* p = str; 
    int i; 
    return parse(p, str + std::wcslen(str), int_, i) && p != str ? i : 0; 
} 

但是請注意,你可能會注意到增加編譯時間(使用預編譯頭推薦)。

2

試試這個:

inline int wtoi(const wchar_t *str) 
{ 
    return (int)wcstol(str, 0, 10); 
}