How to Convert Hex String to Byte Array in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report A Hex String is a combination of the digits 0-9 and characters A-F and a byte array is an array used to store byte data types only. The default value of each element of the byte array is 0. In this article, we will learn how to convert a hex string to a byte array. Example: Input: Hex String : "2f4a33" Output: Byte Array : 47 74 51 Explanation: Hex String: "2f" Therefore, "2f" is converted to 47. (2*161) + (15*160) ) = 32 + 15 = 47 For "4a": (4 * 161 + 10 * 160) = 74 For "33": (3 * 161 + 3 * 160) = 51 Therefore the output is: 47 74 51Converting a Hex String to a Byte Array in C++We can convert a hex string to a byte array in C++ by parsing the hex string and converting every pair of hexadecimal characters into their corresponding byte values. ApproachBreak down the hex string into pairs of hexadecimal characters.Find the decimal value of each character pair.Create a byte array from the hex string decimal values.C++ Program to Covert a Hex String to a Byte ArrayThe below program demonstrates how we can convert a hex string to a byte array in C++. C++ // C++ Program to illustrate how to covert a hex string to a // byte array #include <iostream> #include <string> #include <vector> using namespace std; // Function to convert a hex string to a byte array vector<uint8_t> hexStringToByteArray(const string& hexString) { vector<uint8_t> byteArray; // Loop through the hex string, two characters at a time for (size_t i = 0; i < hexString.length(); i += 2) { // Extract two characters representing a byte string byteString = hexString.substr(i, 2); // Convert the byte string to a uint8_t value uint8_t byteValue = static_cast<uint8_t>( stoi(byteString, nullptr, 16)); // Add the byte to the byte array byteArray.push_back(byteValue); } return byteArray; } int main() { string hexString1 = "2f4a33"; vector<uint8_t> byteArray1 = hexStringToByteArray(hexString1); // Print the input and output cout << "Input Hex String: " << hexString1 << endl; cout << "Output Byte Array: "; for (uint8_t byte : byteArray1) { cout << static_cast<int>(byte) << " "; } return 0; } OutputInput Hex String: 2f4a33 Output Byte Array: 47 74 51 Time Complexity: O(N), here N is the length of the hex string.Auxilliary Space: O(N) Comment More infoAdvertise with us Next Article How to Convert Hex String to Byte Array in C++? R rajpootveerendrasingh36 Follow Improve Article Tags : C++ Programs C++ cpp-array cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read Convert String to Char Array in C++ In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.ExamplesInput: str = "geeksforgeeks"Output 4 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch 4 min read Convert Vector of Characters to String in C++ In this article, we will learn different methods to convert the vector of character to string in C++.The most efficient method to convert the vector of characters to string is by using string's range constructor. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; 2 min read Like