[实用程序源码及工具软件] C++语言下的int2hex()和hex2int()

[复制链接]
 楼主| hotpower 发表于 2023-9-18 22:17 | 显示全部楼层 |阅读模式
  1. #pragma once
  2. #include <iostream>
  3. #include <iomanip> //std::hex, std::setw, std::setfill

  4. template <class T>T hex2int(string hexstr);
  5. template <class T>string int2hex(T val, int size);

  6. template <class T>
  7. T hex2int(string hexstr) {
  8.         istringstream intstream(hexstr);
  9.         T val;
  10.         intstream >> hex >> val;
  11.         return val;
  12. }

  13. template <class T>
  14. string int2hex(T val, int size) {
  15.         ostringstream hexstream;
  16.         hexstream << hex << setw(size) << setfill('0') << setiosflags(ios::uppercase) << val;
  17.         return hexstream.str();
  18. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:[url=http://www.21ic.com/tools/HotWC3_V1.23.html]

1460

主题

21619

帖子

508

粉丝
快速回复 在线客服 返回列表 返回顶部