2015-08-29 73 views
0

這裏是我的代碼:字符串流將unsigned long轉換格式的數字字符串用逗號

unsigned long a_ptr = *(unsigned long*)agent.m_ptr; 
std::string a_type = agTypeToText(agent.GetType()); 
std::string a_cat = agCategoryToText(agent.GetCategory()); 

std::stringstream selInfo; 
selInfo << "Agent pointer: " << a_ptr << "\nAgent type: " << a_type << "\nAgent category: " << a_cat; 

,當我顯示此,a_ptr看起來像834993193,當在現實中我希望它顯示爲十六進制數

,像0xFACEF00D,有或沒有0x。有什麼辦法可以用stringstream來做到這一點?

+0

看看[uintptr_t的(http://stackoverflow.com/questions/1845482/ what-is-uintptr-t-data-type) –

回答

1

是通過傳遞std::hexstringstream

std::stringstream ss; 
unsigned long v = 12345678910; 
ss << std::hex << v; 
std::cout << ss.str() << '\n'; 

或者如果你喜歡大寫數字

ss << std::uppercase << std::hex << v; 
+0

是的,我是這麼認爲的。不過,雖然它現在顯示了正確的十六進制字符,但它們仍以逗號分隔。 – sk099

+0

不適合我。你如何印刷字符串? –

+0

'font.Draw(312,7,0xffffffff,selInfo.str());' 其中'font.Draw'被定義爲: void void :: Draw(float x,float y,DWORD color,std ::字符串格式,...)const' – sk099