//i要轉化的十進制整數,width轉化后的寬度,位數不足則補0 std::string dec2hex(int i, int width) { std::stringstream ioss; //定義字符串流 std::string s_temp; //存放轉化后字符 ioss << std::hex << i; //以十六制形式輸出 ioss >> s_temp; if (width > s_temp.size()) { std::string s_0(width - s_temp.size(), '0'); //位數不夠則補0 s_temp = s_0 + s_temp; //合並 } std::string s = s_temp.substr(s_temp.length() - width, s_temp.length()); //取右width位 return s; }
轉載自:https://my.oschina.net/u/3273849/blog/3102150
