C++ cout打印十六進制

使用printf輸出十六進制

printf("%.2x",...);

 

使用std::cout輸出十六進制且同上格式

std::cout<< std::hex << std::setfill('0') << std::setw(2) << ...

std::hex 十六進制

std::setfill 空白位填充

std::setw 設置最小寬度

示例:

char buf[3] = { 31,99,2 };
	for (int i = 0; i < 3; i++)
		std::cout << std::hex << std::setfill('0') << std::setw(2) << (unsigned int)buf[i] << std::endl;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章