-
Notifications
You must be signed in to change notification settings - Fork 0
js Namespace
Derek Snider edited this page May 19, 2026
·
1 revision
6 JavaScript/web-oriented functions: base64 encoding/decoding, URL encoding/decoding, parseInt with radix support, and JSON serialization.
See also: Namespaces Overview | Other namespaces
| Function | Description | Example |
|---|---|---|
btoa(result, input) |
Base64 encode | js::btoa(encoded, s) |
atob(result, input) |
Base64 decode | js::atob(decoded, encoded) |
| Function | Description | Example |
|---|---|---|
encodeURIComponent(result, input) |
URL-encode a string | js::encodeURIComponent(url, s) |
decodeURIComponent(result, input) |
URL-decode a string | js::decodeURIComponent(s, url) |
| Function | Description | Example |
|---|---|---|
parseInt(str, radix) |
Parse integer with base (2-36) | n = js::parseInt(s, 16) |
Common uses:
-
js::parseInt(s, 16)— hexadecimal -
js::parseInt(s, 8)— octal -
js::parseInt(s, 2)— binary -
js::parseInt(s, 10)— decimal (explicit)
| Function | Description | Example |
|---|---|---|
stringify(result, arr) |
Serialize array to JSON string | js::stringify(json, a) |
Serializes a array to a JSON array string. String values are quoted and escaped, integers and doubles are bare.
#include <iostream>
int main()
{
// base64 round-trip
string original = "Hello, World!";
string encoded;
string decoded;
js::btoa(encoded, original);
js::atob(decoded, encoded);
cout << encoded << endl; // SGVsbG8sIFdvcmxkIQ==
cout << decoded << endl; // Hello, World!
// URL encoding
string query = "name=John Doe&age=30";
string url;
js::encodeURIComponent(url, query);
cout << url << endl;
// hex parsing
string hex = "ff";
int n;
n = js::parseInt(hex, 16);
cout << n << endl; // 255
// JSON
array data;
php::array_push(data, "hello");
php::array_push_int(data, 42);
string json;
js::stringify(json, data);
cout << json << endl; // ["hello",42]
return 0;
}