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++? 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 How to Convert ASCII Value into char in C++? In C++, ASCII (American Standard Code for Information Interchange) values are numerical representations of characters that are used in the C++. In this article, we will learn how to convert an ASCII value to a char in C++. Example: Input: int asciiValue = 65; Output: Character for ASCII value 65 is: 2 min read Convert String to Integer Vector in C++ Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve 2 min read Array of Pointers to Strings in C++ In C++, an array is a homogeneous collection of data that is stored in a contiguous memory location. We can store almost all types of data as array elements. In this article, we will learn how to store the array of pointers to strings in C++. Array of Pointers to Strings in C++A pointer to a string 6 min read Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char* 3 min read Different Ways to Convert Hex String to Integer in C++ STL A hexadecimal number is a number whose base is 16. has numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. And 10, 11, 12, 13, 14, and 15 these numbers are denoted by A, B, C, D, E, F. In C++ STL there are certain properties that help to convert a hexadecimal string or number to a dec 5 min read Like