Convert string to byte array in C++

In this article, we will discuss different ways to convert a string to a byte array in C++.

Table of Contents

Method 1: Using STL ALgorithm transform()

Create a byte array of same length as number of characters in the string, because a character takes 1 byte only. Then pass following arguments to the transform() function,

  • Iterator pointing to the start of string.
  • Iterator pointing to the end of string.
  • Byte array
  • A lambda function that converts a given character to a byte.

The transform() function will pass each each character of string to the given lambda function, and saves the returned byte values to the given byte array. Let’s see an example,

#include <iostream>
#include <cstddef>
#include <algorithm>

int main()
{
    std::string strValue = "Some Text";

    // Create byte array of same size as string length
    std::byte byteArr[strValue.length()];

    // Iterate over characters in string,
    // convert them to byte and copy to byte array
    std::transform(
        strValue.begin(),
        strValue.end(),
        byteArr,
        [](const char& ch) {
            return std::byte(ch);
        });

    // Iterate over byte array and print
    for (const std::byte& byt: byteArr) 
    {
        std::cout << std::to_integer<int>(byt) << ", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

Advertisements
83, 111, 109, 101, 32, 84, 101, 120, 116,

We converted the string to a byte array in C++.

You can compile the above code using,

g++ -std=c++17 temp.cpp

Method 2: Using memcpy() function

From C++11 onwards, all characters in string are stored at continuous memory locations. So we can directly copy the internal memory of string to the byte array using memcpy() function. The data() function, returnes a character pointer to the internal memory, pass that to the memcpy() along with array. Let’s see an example,

#include <iostream>
#include <cstddef>
#include <cstring>

int main()
{
    std::string strValue = "Some Text";

    // Create byte array of same size as string length
    std::byte byteArr[strValue.length()];

    // Copy memory containing characters of string to byte array
    std::memcpy(byteArr, strValue.data(), strValue.length());

    // Iterate over byte array and print
    for (const std::byte& byt: byteArr) 
    {
        std::cout << std::to_integer<int>(byt) << ", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

83, 111, 109, 101, 32, 84, 101, 120, 116,

We converted the string to a byte array in C++.

You can compile the above code using,

g++ -std=c++17 temp.cpp

Method 3: Using for-loop

Iterate over all characters of string using a index based for loop. During iteration, convert each character to byte, and copy it to the byte array. Let’s see an example,

#include <iostream>
#include <cstddef>
#include <algorithm>

int main()
{
    std::string strValue = "Some Text";

    // Create byte array of same size as string length
    std::byte byteArr[strValue.length()];

    // Iterate over characters in string,
    // convert them to byte and copy to byte array
    for (size_t i = 0; i < strValue.length(); i++)
    {
        byteArr[i] = std::byte(strValue[i]);
    }

    // Iterate over byte array and print
    for (const std::byte& byt: byteArr) 
    {
        std::cout << std::to_integer<int>(byt) << ", ";
    }
    std::cout<<std::endl;

    return 0;
}

Output:

83, 111, 109, 101, 32, 84, 101, 120, 116,

We converted the string to a byte array in C++.

You can compile the above code using,

g++ -std=c++17 temp.cpp

Summary

We learned about different ways to convert a string to a byte array in C++. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top