0

I have to convert a string to unsigned char in C++:

typedef unsigned char BYTE;
BYTE Sample1 = 0x01;
string a = "0x08";
BYTE Sample2 = (BYTE)a; //have Store the string in the Sample2 .. but unable to convert

method 2

int main()
{
string hex_str = "0x3d"; 
 // hex value
 unsigned char decimal;
 stringstream my_ss;
 my_ss <<hex<< hex_str;
 my_ss >> decimal;
 cout << "The value is: " << decimal; // it prints 0 not 0x3d

 return 0;
 }

both does not works Help me to resolve this.

BALAJI V S
  • 63
  • 6
  • 2
    You'd likely need to use a `std::stringstream` with `std::hex` to read in that `std::string` into the type you'd expect. A c-style cast is definitely not at all the way to approach this. – Cory Kramer Apr 02 '21 at 17:46
  • 1
    Does this answer your question? [How to convert a string literal to unsigned char array in visual c++](https://stackoverflow.com/questions/2206050/how-to-convert-a-string-literal-to-unsigned-char-array-in-visual-c) – Yash Apr 02 '21 at 17:47
  • i have added some more code snippet that i tried .. is that the way you have specified ??@CoryKramer Does the method 2 helps you to understand??@RemyLebeau – BALAJI V S Apr 02 '21 at 18:11

2 Answers2

2

Your first code doesn't work because there is no standard conversion operator defined for converting a std::string to a unsigned char. You need to actually parse the std::string into a numeric value.

Your second code doesn't work because your std::stringstream is not in std::hex mode when reading decimal from the stream, but even if it were, operator>> treats an unsigned char as a text character, not as an integer (same with operator<< for output). So you need to use an actual integer type, such as short or int, which can hold the full value of the parsed numeric, eg:

string hex_str = "0x3d"; 
unsigned short temp;
stringstream my_ss;
my_ss << hex_str;
my_ss >> hex >> temp;
unsigned char decimal = temp;

That being said, I would suggest using std::istringstream instead, eg:

#include <string>
#include <sstream>
#include <iomanip>

std::string a = "0x08";
unsigned short temp;
std::istringstream(a) >> std::hex >> temp;
BYTE Sample2 = static_cast<BYTE>(temp);

Demo

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    std::string hex_str = "0x3d"; 
    unsigned short temp;
    std::istringstream(hex_str) >> std::hex >> temp;
    unsigned char decimal = static_cast<unsigned char>(temp);
    std::cout << "The value is: " << static_cast<int>(decimal);
    return 0;
}

Demo

Or, in C++11 and later, you can use std::stoi() instead, eg:

#include <string>

std::string a = "0x08";
BYTE Sample2 = static_cast<BYTE>(std::stoi(a, nullptr, 16));

Demo

#include <iostream>
#include <string>

int main()
{
    std::string hex_str = "0x3d"; 
    unsigned char decimal = static_cast<unsigned char>(std::stoi(hex_str, nullptr, 16));
    std::cout << "The value is: " << static_cast<int>(decimal);
    return 0;
}

Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I am not sure if this works for unsigned chars but try using this as a reference

std::string str = "std::string to char*";
 
char* c = const_cast<char*>(str.c_str());
std::cout << c;

also I do not know if this is a typo in your code but change "type def" to "typedef"

Caleb
  • 55
  • 5