2

I have the following:

char const* code = "3D";

I need to convert this 2-digit lexical hex into a std::string, which will be a string with length of 1 (not including null terminator). I have the boost library at my disposal as well. How can I do this?

In the example above, I should have a std::string that prints "=" if properly converted.

1

4 Answers 4

7

I think something on this order should work:

std::istringstream buffer("3D");
int x;

buffer >> std::hex >> x;
std::string result(1, (char)x);

std::cout << result;  // should print "="
Sign up to request clarification or add additional context in comments.

Comments

4

For example, using only standard C++03:

#include <cstdlib>
#include <string>
#include <iostream>

int main() {
  char const* code = "3D";
  std::string str(1, static_cast<char>(std::strtoul(code, 0, 16)));
  std::cout << str << std::endl;
}

In a real application, you'd have to test whether the entire string has been converted (second argument to strtoul) and whether the conversion result is in the allowed range.


Here is a more elaborate example, using C++11 and Boost:

#include <string>
#include <cstddef>
#include <iostream>
#include <stdexcept>

#include <boost/numeric/conversion/cast.hpp>

template<typename T>
T parse_int(const std::string& str, int base) {
  std::size_t index = 0;
  unsigned long result = std::stoul(str, &index, base);
  if (index != str.length()) throw std::invalid_argument("Invalid argument");
  return boost::numeric_cast<T>(result);
}

int main() {
  char const* code = "3D";
  std::string str(1, parse_int<char>(code, 16));
  std::cout << str << std::endl;
}

Comments

3

In Boost release 1.50 (coming this May), you would simply write

string s;
boost::algorithm::unhex ( code, std::back_inserter (s));

Works on std::string, std::wstring, QtString, CString, etc, etc.

Comments

2

It's not C++ but you can still use the good old scanf:

int d;
scanf("%x", &d);

Or from a string using sscanf:

int d;
sscanf(code, "%x", &d);

And using a std::string:

int d;
sscanf(code.c_str(), "%x", &d);

For some case the C format function (scanf & printf families) are easier to use than the object-oriented equivalent.

8 Comments

Shouldn't you provide an sscanf example?
scanf is EVIL, and should be avoided.
@Geoffroy: Maybe an example that works with std::string? You know, that uses c_str?
@Bukes why is it evil? If you know how to handle it, it works well ! And the difference is that it's shorter to write.
@Geoffroy - It's evil for a number of reasons. It does not handle malformed input at all, and you've no way to recognize this. Many others more distinguished than I agree - scanf is evil. c-faq.com/stdio/scanfprobs.html, stackoverflow.com/questions/456303/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.