【问题标题】:Hex byte values to DWORD (ascii) format十六进制字节值到 DWORD (ascii) 格式
【发布时间】:2014-12-22 06:39:03
【问题描述】:

需要从字节值转换为 DWORD(基于 ascii)!

示例:

输入:1F 12 BA 43(保存在std::vector<BYTE>

输出:31 46 31 32 42 41 34 33(输入的 ascii 值)-> 也存储在 std::vector<BYTE>

进行这种转换的最简单方法是什么?

谢谢

【问题讨论】:

    标签: c++ byte ascii dword


    【解决方案1】:
    typedef unsigned char BYTE;
    typedef std::vector<BYTE> Vector;
    
    Vector hex2ascii(Vector const &src) {
      Vector result;
      for ( Vector::const_iterator iter = src.begin(); iter!=src.end(); iter++ ) {
        BYTE item = *iter;
        unsigned bits=8;
        do {
          bits -= 4;
          result.push_back("0123456789ABCDEF"[(item>>bits)&15u]);
        } while(bits);
      }
      return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 2015-03-29
      • 2015-01-13
      • 2013-05-29
      • 2014-01-02
      • 2017-09-13
      • 2011-12-09
      • 2011-04-14
      • 2015-08-08
      相关资源
      最近更新 更多