How to Convert String to Hex in C++

Jinku Hu Feb 02, 2024
  1. Use std::cout and std::hex to Convert String to Hexadecimal Value in C++
  2. Use std::stringstream and std::hex to Convert String to Hexadecimal Value in C++
How to Convert String to Hex in C++

This article will demonstrate multiple methods about how to convert string to hex in C++.

Use std::cout and std::hex to Convert String to Hexadecimal Value in C++

Hexadecimal notation is a common format for reading binary files representing program files, encoded format, or just text. Thus, one would need to generate file contents with hexadecimal data and output it as needed.

In this example, we output the stored string object as hexadecimal characters to the console. Note that C++ provides a std::hex I/O manipulator that can modify the stream data’s number base. A string object should be decomposed as single characters and then individually modified with std::hex to their respective hexadecimal representation. We implement the range-based loop to iterate over string characters and redirect modified data to the cout stream.

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using std::hex;
using std::string;
using std::stringstream;

int main() {
  string s1 = "This will be converted to hexadecimal";
  string s2;

  cout << "string: " << s1 << endl;
  cout << "hexval: ";
  for (const auto &item : s1) {
    cout << hex << int(item);
  }
  cout << endl;

  return EXIT_SUCCESS;
}

Output:

string: This will be converted to hexadecimal
hexval: 546869732077696c6c20626520636f6e76657274656420746f2068657861646563696d616c

Use std::stringstream and std::hex to Convert String to Hexadecimal Value in C++

The previous method lacks the feature of storing the hexadecimal data in the object. The solution to this issue is to create a stringstream object, where we insert the hexadecimal values of string characters using the iteration. Once the data is in stringstream, it can construct a new string object for storing modified character data.

Notice that data can be outputted directly from the stringstream object, but in the following example code, a more simple form - cout << string is employed. An alternative end-use case could be to write the hexadecimal data directly into the file, using filesystem utilities of the standard library.

#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using std::hex;
using std::string;
using std::stringstream;

int main() {
  string s1 = "This will be converted to hexadecimal";
  string s2;
  stringstream ss;

  cout << "string: " << s1 << endl;

  for (const auto &item : s1) {
    ss << hex << int(item);
  }
  s2 = ss.str();
  cout << "hexval: " << s2 << endl;

  return EXIT_SUCCESS;
}

Output:

string: This will be converted to hexadecimal
hexval: 546869732077696c6c20626520636f6e76657274656420746f2068657861646563696d616c
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ String