C++ VECTORS

Hello, how's everybody going?.

I am trying to copy exactly as it is, a vector<std::string> to a vector<byte> without changing/casting any element. What do i mean? For example this is what it is contained in my vector<std::string>:

Element 1: AC
Element 2: 9C
Element 3: FA
Element 4: BC
etc..

How could i possibly copy the exact same elements on a vector<byte> ?

Thanks.
closed account (48bpfSEw)
no way without casting
and "AC" is not equal to $AC
you have to convert a string into hex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using byte = unsigned char ;

std::vector<byte> to_bytes( const std::vector<std::string>& str_seq )
{
    std::vector<byte> byte_seq ;

    for( const std::string& str : str_seq )
    {
        const int int_val = std::stoi( str, nullptr, 16 ) ;

        if( int_val < 0 || int_val > std::numeric_limits<byte>::max() )
            throw std::out_of_range( "value is outside the range of byte" ) ;

        byte_seq.push_back(int_val) ;
    }

    return byte_seq ;
}

http://coliru.stacked-crooked.com/a/0690735368015bbd
Topic archived. No new replies allowed.