Convert any type to byte array

I need help converting any type to a byte array.

I thought of writing the following code but I am pretty sure that it wont work.

1
2
3
4
5
6
7
  template<typename T>
byte &toByteArray(T t){
	byte *bytes = byte[sizeof T];
	bytes = (byte) t;
        return bytes;
};
Last edited on
In Qt, QByteArray can loosely be viewed as an implementation/wrapper for char const *, what is your own implementation of ByteArray? Why would you want to convert ANY type to a byteArray? Perhaps your only solution might not be the only one after all
Last edited on
I have just learned C++ and I am trying to give myself something to do so I decided to try make something that encrypts bytes of values to make it more difficult for other people to read the process memory ^~^
We can examine objects of any type as arrays of bytes.

However, to be able to restore the value from an array of bytes:
Objects of trivially-copyable types are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write()/std::ifstream::read(). In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original. http://en.cppreference.com/w/cpp/types/is_trivially_copyable


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <array>
#include <memory>
#include <type_traits>
#include <iostream>
#include <iomanip>

using byte = unsigned char ;

template< typename T > std::array< byte, sizeof(T) >  to_bytes( const T& object )
{
    std::array< byte, sizeof(T) > bytes ;

    const byte* begin = reinterpret_cast< const byte* >( std::addressof(object) ) ;
    const byte* end = begin + sizeof(T) ;
    std::copy( begin, end, std::begin(bytes) ) ;

    return bytes ;
}

template< typename T >
T& from_bytes( const std::array< byte, sizeof(T) >& bytes, T& object )
{
    // http://en.cppreference.com/w/cpp/types/is_trivially_copyable
    static_assert( std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type" ) ;

    byte* begin_object = reinterpret_cast< byte* >( std::addressof(object) ) ;
    std::copy( std::begin(bytes), std::end(bytes), begin_object ) ;

    return object ;
}

int main()
{
    double d = 123.456789 ;
    const auto bytes = to_bytes(d) ;

    std::cout << std::hex << std::setfill('0') ;
    for( byte b : bytes ) std::cout << std::setw(2) << int(b) << ' ' ;
    std::cout << '\n' ;

    d = 0 ;
    from_bytes( bytes, d ) ;
    std::cout << std::fixed << d << '\n' ;

    int arr[] = { 1, 63, 256, 511, 1024 } ;
    const auto array_bytes = to_bytes(arr) ;

    for( byte b : array_bytes ) std::cout << std::setw(2) << int(b) << ' ' ;
    std::cout << '\n' ;
    
    for( int& v : arr ) v = -1 ;
    from_bytes( array_bytes, arr ) ;
    for( int v : arr ) std::cout << std::dec << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/959c715b10d4d439
My Microsoft Visual Studio 10.0 swears so on Error 2 error C2873: byte: the use of a character in "using" -obyavlenii not allowed

using byte = unsigned char;

What is it really for?
I am a beginner in programming, and indeed barely familiar with C ++.


Last edited on
And is there what type of project for compiling of it, it is better to choose it would be?
> What is it really for?

It creates a type alias; byte becomes another name for the type unsigned char.
std::array< byte, sizeof(T) > is a wee bit easier on the eye than std::array< unsigned char, sizeof(T) >.
Though both mean the the same thing.

The classical C++ mechanism to create a type alias is typedef.
typedef unsigned char byte ;
http://en.cppreference.com/w/cpp/language/typedef

Type aliases came with C++11.
using byte = unsigned char ;
http://en.cppreference.com/w/cpp/language/type_alias

Visual Studio 10.0 is fairly old now; consider switching to a more recent version of Visual Studio.
Спасибі велічезніше!! Я дуже вдячний!!
Thank you velicheznishe!! I am very thankful!!

Topic archived. No new replies allowed.