Home Contents | # magstripe.encode | Magstripe | Core4 Lua Event System

## SYNOPSIS

rawdata = magstripe.encode(tracknr, data[, flags])

## DESCRIPTION

This function takes an ASCII encoded string and turns it into raw magstripe data bitsthe raw magnetic stripe bits according to the ISO standard encoding for track tracknr.

The encoder checks if the passed string already includes the start, stop and checksum characters. If not, these characters are calculated and automatically added to the result data.

The function fails (returns nil) if the resulting binary data would exceed 256 bytes. It also fails if one of the characters in the source string is not encodable to the requested standard.

Each character in the string flags controls the bit order of the bitstream:

- '7': First bit of trackdata is written to bit 7 of the first byte. (Otherwise, first bit is written to bit 0.)

The flags parameter defaults to an empty string when not specified.

This is the reverse function of magstripe.decode(). In comparison, please note the encoder always encodes in forward direction. The flags 'F' and 'R' that are understood by the decoder have no effect on the encoder.

## RETURN VALUE

On success, returns a string with the encoded data, including the start, end and checksum bits. On failure, returns nil.

## SEE ALSO

magstripe.decode