Hex byte values to DWORD (ascii) format

732 views Asked by At

Need a conversion from byte values to DWORD (ascii based)!

Example:

INPUT: 1F 12 BA 43 (saved in std::vector<BYTE>)

OUTPUT: 31 46 31 32 42 41 34 33 (ascii values of input) -> also stored in a std::vector<BYTE>

Easiest method to do this conversion?

Thx

1

There are 1 answers

0
Hans Klünder On BEST ANSWER
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;
}