class::calc::Base64

class calc::Base64
  : public api::InfoObject

This class includes methods to encode and decode data using the base64 algorithm. Base64 is useful for representing binary data using text characters. This can be useful when transmitting data over certain serial links that do not support binary transfers.

The following example can be used to encode and decode using Base64.

First, include the Stratify API headers that we will need for these examples.

//md2code:include
#include <sapi/calc.hpp>
#include <sapi/var.hpp>
#include <sapi/fs.hpp>

Now do some encoding and decoding like a boss.

//md2code:main
Data data_to_encode(arg::Size(128));
data_to_encode.fill<u8>(32);

String encoded_string = Base64::encode(
  arg::SourceData(data_to_encode)
);

//You can then decode the data using this code snippet:

Data original_data = Base64::decode(
  arg::Base64EncodedString(encoded_string)
);

if( original_data == data_to_encode ){
  printf("It works!\n");
}

Summary

static var::Data decode(

const arg::ImplicitBase64EncodedString input
)

static int decode(

const arg::SourceFile input,
const arg::DestinationFile output,
const arg::Size size
)

static var::String encode(

const arg::ImplicitSourceData input
)

static int encode(

const arg::SourceFile source,
const arg::DestinationFile destination,
const arg::Size size
)

Members

static var::Data decode(

const arg::ImplicitBase64EncodedString input
)

Decodes base64 encoded data.

Returns

Zero

//md2code:main
String encoded_string;
//assign base64 encoded string
Data raw_data;

raw_data = Base64::decode(encoded_string);

static int decode(

const arg::SourceFile input,
const arg::DestinationFile output,
const arg::Size size
)

Reads base64 encoded data from input and writes raw, decoded data to output.

Returns

Number of decoded bytes written to output

The method reads size bytes (or to EOF if size is zero) from input start at the current location. The output string is written to output at the current location.


static var::String encode(

const arg::ImplicitSourceData input
)

Encodes data to the base64 format.

Returns

Number of bytes in the encoded string

//md2code:main
Data raw_data(arg::Size(64)); //raw binary data that needs to be encoded
raw_data.fill<u8>(0xaa);
String result = Base64::encode(arg::SourceData(raw_data));
printf("Encoded string is '%s'\n", result.cstring());

static int encode(

const arg::SourceFile source,
const arg::DestinationFile destination,
const arg::Size size
)

Reads binary data from input and writes a Base64 encoded string to output.

Returns

Number of encoded bytes written to output

The method reads size bytes (or to EOF if size is zero) from input start at the current location. The output string is written to output at the current location.

//md2code:main
File source;
File destination;

source.open(
  arg::FilePath("/home/raw_data.dat"),
  OpenFlags::read_only()
  );

destination.create(
  arg::DestinationFilePath("/home/base64_encoded.txt"),
  arg::IsOverwrite(true)
  );

Base64::encode(
  arg::SourceFile(source),
  arg::DestinationFile(destination)
  );

X

Thanks for Coming!

Subscribe to news and updates