Skip to content

how to: text binary conversion

amir zamani edited this page Aug 5, 2016 · 1 revision

mbedcrypto supports base64 and hex encoding and decoding.

using namespace mbedcrypto;

std::fstream fpng = open_a_png_file(...);
std::string bin_data; // binary data
fpng >> bin_data;

auto png_hex = to_hex(bin_data);
auto png_b64 = to_base64(bin_data);
REQUIRE( from_hex(png_hex) == from_base64(png_b64) );

QByteArray srcBase64 = ...;
QByteArray source    = from_base64(sourceBase64);
auto cloneBase64     = to_base64(source);
REQUIRE( srcBase64 == cloneBase64 );

// invalid base64 decoding throws:
REQUIRE_THROWS( base64::decode("invalid base64 string @#$%#^$") );

to get the encoding/decoding size in advance:

// to get the required base64 size
size_t en_size  = base64::encode_size(src_binary);
auto src_base64 = base64::encode(src_binary);
REQUIRE( src_base64.size() == en_size );

see tcodec.hpp

Clone this wiki locally