Click here to Skip to main content
15,607,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are many ways to convert SINGLE ASCII to hex...
how about converting this

ASCII ((QString) "42" to

two 8 bit word(s) "42" ?

What I have tried:

This is over my head...I have no idea how to approach this issue ( and just filling the required number of characters here....
Updated 23-Jan-23 8:22am

Do you mean something similar to
#include <iostream>
#include <sstream>
using namespace std;

std::string to_hex(std::string s )
  std::ostringstream oss;

  for (auto c: s)
    oss << std::hex << (unsigned int) c;
  return oss.str();

int main()
  std::string s{"foo"};
  std::cout << s << ", " << to_hex(s) << std::endl;

Share this answer
Simple: take each Ascii character numeral and subract '0' - that will give you a value between 0 and 9.
char num[] = "42";
int val = (num[0] - '0') * 10 + (num[1] - '0');
Will give you the value 42.
Share this answer
Yount_0701 24-Jan-23 21:06pm    
how about strtol, but the return value is an 8 bytes datatype(long). Anyway the two-byte variable also has an overflow risk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900