1

so i have a string like this:std::string MyString = "\\xce\\xc6";

where when i print it like this:std::cout << MyString.c_str()[0] << std::endl;

as output i get:\

and i want it to be like this:std::string MyDesiredString = "\xce\xc6";

so when i do:

std::cout << MyDesiredString.c_str()[0] << std::endl;
// OUTPUT: \xce (the whole byte)

so basically i want to identify the string(that represents bytes) and convert it to an array of real bytes

i came up with a function like this:

// this is a pseudo code i'm sure it has a lot of bugs and may not even work
// just for example for what i think
char str_to_bytes(const char* MyStr) { // MyStr length == 4 (\\xc6)
  std::map<char*, char> MyMap = { {"\\xce", '\xce'}, {"\\xc6", 'xc6'} } // and so on
  return MyMap[MyStr]
}
//if the provided char* is "\\xc6" it should return the char '\xc6'

but i believe there must be a better way to do it.

as much as i have searched i haven't found anything useful

thanks in advance

  • There's no need for mapping of any kind. – πάντα ῥεῖ Apr 29 '21 at 12:41
  • @πάνταῥεῖ why'd you close my question. the uint8_t doesn't help me cause i have a string \xce that prints like that and i want to get the byte version \xce that i don't really know if it is even visible. as i am new to C++ it uint8_t may be what i need but i don't really know what and how to do it. please give me an example – TheProgramer Apr 29 '21 at 12:48
  • 1
    With `std::string MyString = "\\xce\\xc6"`, you have a string of 8 characters: a backslash `'\'`, letter `x`, letter `c`, letter `e`, another backslash, letter `x`, letter `c`, digit `6`. So `MyString[0]` is the backslash. With `std::string MyDesiredString = "\xce\xc6";` you have a string of two characters with ASCII codes `0xCE` and `0xC6` correspondingly. Are you saying you want to convert the former to the latter? Then you'd have to write a parser to parse and transform C-style escape sequences, the same way the compiler does. – Igor Tandetnik Apr 29 '21 at 13:34
  • @IgorTandetnik yeah, i want the text \\xce to be converted to \xce but i can't think of a better way than to use a map. any idea? – TheProgramer Apr 29 '21 at 13:43
  • Are you asking how to [convert a hexadecimal string to a number](https://stackoverflow.com/questions/45353171/convert-hexadecimal-string-to-decimal-number-in-c)? – Igor Tandetnik Apr 29 '21 at 13:46
  • @IgorTandetnik i don't think that helps, but i'll give it a try – TheProgramer Apr 29 '21 at 13:48
  • @IgorTandetnik nope, that doesn't help. when i try to pass "\\xce" as parameter it throws an exception(std::invalid_argument) – TheProgramer Apr 29 '21 at 13:55
  • What happens if you pass `"ce"` as parameter? – Igor Tandetnik Apr 29 '21 at 13:58

1 Answers1

0

Try something like this:

std::string teststr = "\\xce\\xc6";
std::string delimiter = "\\x";
size_t pos = 0;
std::string token;
std::string res;
while ((pos = teststr.find(delimiter)) != std::string::npos) {
    token = teststr.substr(pos + delimiter.length(), 2);
    res.push_back((char)stol(token, nullptr, 16));
    std::cout << stol(token, nullptr, 16) << std::endl;
    teststr.erase(pos, pos + delimiter.length() + 2);
}
std::cout << res << std::endl;

Take your string, split it up by the literals indicating a hex. value is provided (\x) and then parse the two hex. characters with the stol function as Igor Tandetnik mentioned. You can then of course add those byte values to a string.

ThomasW
  • 56
  • 1
  • 7