# B64ENC

> [HTML Version](b64encsbr.htm)

_Updated October 2017_

**xcall B64ENC, infile, och, status \{,flag\}**

**xcall B64ENC, indata, outdata, status, flag**

B64ENC provides Base 64 (aka MIME) encoding and decoding. The first format encodes a file, writing the encoded output to another file, while the second format may be used for encoding and decoding of memory buffers.  

**Parameters**

_infile_  ([String](string.md))  \[in\]

Specification of file to encode in AMOS or native format

_och_  ([Num](num.md)) \[in\]

Channel of a file, previously opened for output, to which the encoded version of the input file will be written. The file remains open on return.

_status  _(Signed [Num](num.md))  \[out\]

On return, will be set to the number of bytes written, or <0 for errors

| **Value** | **Description** |
|------|------|
| \>=0 | Number of bytes written |
| \-1 | _infile_ not found |
| \-2 | unable to open _infile_ |
| \-3 | _och_ not valid (not open for OUTPUT) |
| \-4 | file error during input of file |
| \-5 | error during output |
| \-6 | _outdata_ too small to hold encrypted output |




_indata  _(X or [String](string.md))  \[in\]

Variable containing data to encode or decode

_outdata_  (X or [String](string.md))  \[out\]

Variable containing encoded or decoded version of _indata_.  Since encoded data is 4/3 larger than raw data, the determination of whether the operation is encode or decode is based on the relative sizes of the _indata_ and _outdata_ parameters. If the physical variable _outdata_ is larger than _indata_, then the operation will be to encode; otherwise it will be to decode. If using a dynamic string (S,0), you will need to initialize it so that it's effective size is greater than or less than the size of the indata string, depending on whether you are encoding or decoding. See the BASIC string function FILL\$() for a convenient way to do this.

_flag  _([Num](num.md))  \[in\]

If not specified, or set to 0, the format is assumed to be that given in the first syntax line above (encode a file).  Otherwise, to encode a memory buffer, _flag _must be set to the number of bytes in _indata_ to encode.  (Since raw data may contain binary values, including 0, we can not use the normal string length method of determining the logical size of the input data.)  To decode data, _flag _should be set to 1.

**Comments**

Base 64 encoded data uses only 64 distinct bytes (all printable) for representation, which is often convenient for including binary data within formats that are otherwise string-oriented (such as email). Because only six bits are used in each byte of the encoded representation, the encoded version of the data is 4/3 larger than the raw data. The raw data does not have to be a multiple of three.

When using the first forma, to encode to an output file, the base64 data is split into lines of 72 characters. The decoding routine, however, expects the base64 data in the _indata_ parameter to be a single linear string with no line breaks. 

See the [module fnb64.sbi in SOSLIB:\[907,10\]](https://bitbucket.org/microsabio/soslib/src/master/907010/fnb64.bsi) for a collection of convenience function wrappers for B64ENC.

Also, for a more symmetrical implementation of base64 encoding/decoding, including other encoding formats such as hex, see [CRYPTO](cryptosbr.md).