_strFromCPP
parse C++ zero-terminated string and return Javascript-string
Requires
import {_strFromCPP} from "./tt/ttWASM.js";
Definition
function _strFromCPP(cppStr)
Description
Javascript strings are same as almost everything else Objects
. However a C++ string is an array of characters ending on character-code 0
. _strFromCPP
converts a C++ zero terminated string to a Javascript String
Parameters
cppStr
: A char*
formatted zero terminated C++ String.
Return Value
[String]
Remarks
-
Example: decimal to hexadecimal converter in C++
main.js:
import {_wasmInitA, _strFromCPP} from "./tt/ttWASM.js";
import {_newTag, _render} from "./tt/ttHTML.js";
import wasm from "./web.wasm";
async function main()
{
const num = 169;
var str;
const wasmInstance = await _wasmInitA(wasm, {}, null);
str = _strFromCPP(wasmInstance.exports.toHex(num));
_render(<h1>The decimal byte-value: {num} is {str} in hexadecimal</h1>);
}
main();
cppExample.cpp:
#include "tiktag.h"
char _string[3];
_export char* toHex(unsigned char inByte)
{
char htab[17] = "0123456789ABCDEF";
_string[0] = htab[((inByte&0xf0)>>4)];
_string[1] = htab[(inByte&0x0f)];
_string[2] = 0;
return (char*) _string;
}
Output:
