varstruct-cstring

Encode / decode C strings.

Usage no npm install needed!

<script type="module">
  import varstructCstring from 'https://cdn.skypack.dev/varstruct-cstring';
</script>

README

varstruct-cstring

npm Build Status JavaScript standard style

Easily encode / decode strings as C strings, that is, null-terminated \0. This is useful to retain compatibility with data structures written in other languages.

Install

npm i --save varstruct #must install varstruct first
npm i --save varstruct-cstring

Example

const vstruct = require('varstruct')
const cstring = require('varstruct-cstring')

const strings = vstruct([
  { name: 'first', type: cstring(16) },
  { name: 'last', type: cstring(16) }
])

const data = {
  first: 'Satoshi',
  last: 'Nakamoto'
}

const buffer = strings.encode(data)
console.log(buffer.toString('hex')) // => '5361746f7368690000000000000000004e616b616d6f746f0000000000000000'

const decodedData = strings.decode(buffer)
console.dir(decodedData) // => { first: 'Satoshi', last: 'Nakamoto' }

License

MIT Copyright Exodus Movement, Inc. 2016