Skip to content

A node module that preforms the function of the bash command "xxd -i".

License

Notifications You must be signed in to change notification settings

James-Oswald/Node-xxdi

Repository files navigation

Node-xxdi

A node module that preforms the function of the bash command "xxd -i".

Background

In trying to reduce tooling dependancies for my Lightweight Jar to Exe program, I developed this package to remove the need for the command "xxd -i" on path which would need MSYS or other alternitives to have on path.

Install

Set registry as https://npm.pkg.github.com/@james-oswald then run npm i xxdi

Useage

This module exposes two funtions:

header(inputFileName: string, callback: (headerText: string) => void, outputFileName: string, variableName: string): void

Asynchronously generates the .h file, callback can be used to get the text contents of the .h file, if outputFileName is provided, will atomatically generate the .h file on the given path. and

headerSync(inputFileName: string, outputFilename: string, variableName: string): string

Synchronously generates the .h file. If outputFileName is provided, will atomatically generate the .h file on the given path.

parameters

inputFileName is the path to the file to be converted to a C hexdump header
callback is the callback from header with the header's text passed in as headerText
outputFileName is the path to the output .h file, null by default, if null, no file is created and the headers text will be passed to the callback or returned in the sync case.
variableName is the name of the C/C++ const char* variable in the .h file, if unprovided will be a legal version of the inputFileName, if the name provided is an illegal C idenifyer, an exception will be thrown.

Sample code

Found in /examples/ helloWorld.txt

Hello World!

index.js

let xxdi = require("xxdi");

//log header text sync
let xxdi = require("xxdi");

//log header text sync
console.log(`sync, default var name:\n${ xxdi.headerSync("helloWorld.txt") }\n`);

//log header text async
xxdi.header("helloWorld.txt", (s)=>{console.log(`async, default var name:\n${ s }\n`);});

//log header text, custom C variable name sync
console.log(`sync, custom var name:\n${ xxdi.headerSync("helloWorld.txt", null, "hello") }\n`);

//log header text, custom C variable name async
xxdi.header("helloWorld.txt", (s)=>{console.log(`async, custom var name:\n${ s }\n`);}, null, "hello");

//create header with custom C var name file sync 
xxdi.headerSync("helloWorld.txt", "helloWorld.h", "hello");

//create header with custom C var name file async 
xxdi.header("helloWorld.txt", (s)=>{}, "helloWorld.h", "hello");

console output

const unsigned char helloWorld_txt[] = {0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x21};
const int helloWorld_txt_len = 12;

sync, custom var name:
const unsigned char hello[] = {0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x21};
const int hello_len = 12;

async, default var name:
const unsigned char helloWorld_txt[] = {0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x21};
const int helloWorld_txt_len = 12;

async, custom var name:
const unsigned char hello[] = {0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x21};
const int hello_len = 12;

helloWorld.h

const unsigned char hello[] = {0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x21};
const int hello_len = 12;

About

A node module that preforms the function of the bash command "xxd -i".

Topics

Resources

License

Stars

Watchers

Forks