This post will discuss how to convert byte array to string in C/C++.

1. Using memcpy() function

The memcpy() function performs a binary copy of the arrays of POD (Plain Old Data) type like int, char, etc. It can be used to convert a byte array to a C-string, as follows. Note that C-Strings are NULL-terminated. Therefore, don’t forget to allocate space for a trailing NULL byte.

Download  Run Code

Output:

Hello

2. Using String Constructor

To construct a C++ string from a byte array, use the string constructor. The constructor string (const char* b, size_t n) copies the first n characters from array b. The following is a simple example demonstrating its usage.

Download  Run Code

Output:

Hello

That’s all about converting byte array to string in C/C++.