# binstring

> Convert binary data to and from various string representations

Latest version **0.2.1** (published 2014-04-13) · 0 weekly downloads

## Install

```sh
npm install binstring
pnpm add binstring
yarn add binstring
bun add binstring
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2014-04-13 |
| First published | 2014-01-02 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Maintainers | midnightlightning, jp, sidazhang |
| Keywords | string, strings, convert, hex, bytes |

## Links

- npm: https://www.npmjs.com/package/binstring
- Repository: https://github.com/cryptocoinjs/binstring
- Issues: https://github.com/cryptocoinjs/binstring/issues
- npm.io page: https://npm.io/package/binstring

## Alternatives

- [@mce/gif](https://npm.io/package/@mce/gif.md) — 2.6K weekly downloads
- [cleanse](https://npm.io/package/cleanse.md) — 173 weekly downloads
- [str](https://npm.io/package/str.md) — 127 weekly downloads
- [naming](https://npm.io/package/naming.md) — 95 weekly downloads
- [tap-telco-api](https://npm.io/package/tap-telco-api.md) — 19 weekly downloads

## Recent versions

- 0.2.1 (latest) — 2014-04-13
- 0.2.0 — 2014-01-20
- 0.1.0 — 2014-01-02

## README

# BinString

JavaScript component to convert to/from strings and byte arrays.

AMD/CommonJS compatible.


## Usage

This library exposes a single function that takes a data parameter and an options object. Set `options.in` and `options.out` to the format of the input and desired output. Possible values include:

* `hex`: Hex-encoded string; two hexadecimal characters per byte
* `binary`: Binary-encoded string (ASCII-encoded)
* `utf8`: Binary-encoded string (UTF8-encoded)
* `bytes`: Byte array; an array of numbers, each representing one byte of data
* `buffer`: A Node.js native Buffer object

The default encoding for `options.out` is a buffer (`buffer`). The input format is duck-typed if it's an Array (`bytes`) or Buffer (`buffer`) or Number object. If it's a string, it's interpreted as `binary` unless it's prefixed by `0x` (then it's `hex`). If `options.in` is set, it overrides the automatic duck-typing of the input variable.

```js
var conv = require('binstring');

console.dir(conv('hello', { in:'binary' })); // No output encoding specified, defaults to Buffer; output: Buffer([104,101,108,108,111])
console.dir(conv([104,101,108,108,111], { out:'hex' })); // No input encoding specified, auto-detected as Byte Array; output: 68656c6c6f
console.dir(conv('hello', { in:'binary', out:'hex' })); // output: 68656c6c6f
```

## Test

Unit tests are written in [Mocha](http://visionmedia.github.io/mocha/). To run the test suite, checkout the git repository, and from within the base folder run:

```sh
$ npm install --dev
$ ./node_modules/mocha/bin/mocha
```

## References on JavaScript UTF-8 forced encoding
(these sources are also included as PDFs in the repo in case the links go dead)

- http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
- http://hossa.in/2012/07/20/utf-8-in-javascript.html

# License
(MIT License)

Copyright 2014, Brooks Boyd <boydb@midnightdesign.ws>

---
_Source: https://npm.io/package/binstring · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
