![]() |
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 51 Converting 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. Approach
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++
Output
Input Hex String: 2f4a33 Output Byte Array: 47 74 51 Time Complexity: O(N), here N is the length of the hex string. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |