AceCommon  1.5.2
Arduino library for low-level common functions and features with no external dependencies
Functions
url_encoding.h File Reference

Provides 2 functions to perform URL form encoding and decoding. More...

#include <stdint.h>
Include dependency graph for url_encoding.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ace_common::formUrlEncode (Print &output, const char *str)
 Encode the str using an encoding suitable for GET parameters and forms in the body of a POST that expects a application/x-www-form-urlencoded type. More...
 
void ace_common::formUrlDecode (Print &output, const char *str)
 Decode the str that was encoded using form_url_encode().
 
void ace_common::byteToHexChar (uint8_t c, char *high, char *low, char baseChar='A')
 Convert a byte into 2-digit, uppercase, hex. More...
 
uint8_t ace_common::hexCharToByte (char c)
 Convert a hex character [0-9a-fA-F] into its integer value. More...
 

Detailed Description

Provides 2 functions to perform URL form encoding and decoding.

A space ‘’ 'is converted into a'+'`, and all other non-alphabetical characters are converted into %{hex}.

Definition in file url_encoding.h.

Function Documentation

◆ byteToHexChar()

void ace_common::byteToHexChar ( uint8_t  c,
char *  high,
char *  low,
char  baseChar = 'A' 
)

Convert a byte into 2-digit, uppercase, hex.

For example, 0x9A returns the '9' and 'A' characters.

I tried returning the (high, low) pair as a uint16_t type, then breaking apart the 2 bytes in the calling routine. But that alternative was slower than using a reference to code0 and code1. I suspect that the compiler is able to optimize away the reference and possibly inline the entire function into the calling code.

Parameters
cthe 8-bit unsigned value to be converted into hexadecimal
highoutput parameter of the high character
lowoutput parameter of the low character
baseCharthe base character of the hexadecimal range, default 'A' which returns hexadecimal characters using uppercase. Set this to 'a' to get lowercase characters.

Definition at line 76 of file url_encoding.cpp.

◆ formUrlEncode()

void ace_common::formUrlEncode ( Print &  output,
const char *  str 
)

Encode the str using an encoding suitable for GET parameters and forms in the body of a POST that expects a application/x-www-form-urlencoded type.

A ' ' is converted into a '+' and non-alphanumerics are percent-encoded.

The result is printed to the output that implements the Print interface. The output could be the Serial object, but more frequently, it is useful to use an in-memory buffer such as PrintStr. This allows us to avoid using a String object, which decreases the risk of heap fragmentation.

See https://en.wikipedia.org/wiki/Percent-encoding and https://stackoverflow.com/questions/1634271.

Definition at line 30 of file url_encoding.cpp.

◆ hexCharToByte()

uint8_t ace_common::hexCharToByte ( char  c)

Convert a hex character [0-9a-fA-F] into its integer value.

For example, 9 returns '9', and 10 returns 'A'. Characters outside of the valid hexadecimal character range return 0.

Definition at line 83 of file url_encoding.cpp.