1
|
|
|
/* |
2
|
|
|
* to-bytes: convert bytes to numbers and strings. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const intBits = require("int-bits"); |
|
|
|
|
8
|
|
|
const pad = require("../src/byte-padding.js"); |
9
|
|
|
const endianness = require("endianness"); |
10
|
|
|
const writer = require("../src/write-bytes.js"); |
11
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Turn numbers and strings to bytes. |
15
|
|
|
* @param {!Array<number>|string} numbers float64 numbers. |
16
|
|
|
* @param {number} bitDepth The desired bitDepth for the data. |
17
|
|
|
* @param {Object} params The params. defaults to: |
18
|
|
|
* - "float", defaults to false, true for floats. |
19
|
|
|
* float is available for 16, 32 and 64 bit depths. |
20
|
|
|
* - "base", defaults to 10, can be 2, 10 or 16 |
21
|
|
|
* - "char", defaults to false, true for strings |
22
|
|
|
* - "be", defaults to false, true for big endian |
23
|
|
|
* @return {!Array<number>} the bytes. |
24
|
|
|
*/ |
25
|
|
|
function toBytes(numbers, bitDepth, params={}) { |
26
|
|
|
let base = 10; |
27
|
|
|
if ("base" in params) { |
28
|
|
|
base = params.base; |
29
|
|
|
} |
30
|
|
|
let isBigEndian = params.be; |
31
|
|
|
let isChar = params.char; |
32
|
|
|
let isFloat = params.float; |
33
|
|
|
let bytes = writeBytes(numbers, isChar, isFloat, isBigEndian, bitDepth); |
34
|
|
|
outputToBase(bytes, bitDepth, base); |
35
|
|
|
return bytes; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Turn the output to the correct base. |
40
|
|
|
* @param {!Array<number>} bytes The bytes. |
41
|
|
|
* @param {number} bitDepth The bitDepth of the data. |
42
|
|
|
* @param {number} base The base. |
43
|
|
|
*/ |
44
|
|
|
function outputToBase(bytes, bitDepth, base) { |
45
|
|
|
if (bitDepth == 4) { |
46
|
|
|
bytesToBase(bytes, base, pad.paddingNibble); |
47
|
|
|
} else if (bitDepth == 2) { |
48
|
|
|
bytesToBase(bytes, base, pad.paddingCrumb); |
49
|
|
|
} else if(bitDepth == 1) { |
50
|
|
|
bytesToBase(bytes, base, function(){}); |
51
|
|
|
}else { |
52
|
|
|
bytesToBase(bytes, base); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Write values as bytes. |
58
|
|
|
* @param {!Array<number>|string} numbers The values. |
59
|
|
|
* @param {boolean} isChar True if it is a string. |
60
|
|
|
* @param {boolean} isFloat True if it is a IEEE floating point number. |
61
|
|
|
* @param {boolean} isBigEndian True if the bytes should be big enadian. |
62
|
|
|
* @param {number} bitDepth The bitDepth of the data. |
63
|
|
|
* @return {!Array<number>} the bytes. |
64
|
|
|
*/ |
65
|
|
|
function writeBytes(numbers, isChar, isFloat, isBigEndian, bitDepth) { |
66
|
|
|
let bitWriter; |
67
|
|
|
if (isChar) { |
68
|
|
|
bitWriter = writer.writeString; |
69
|
|
|
} else { |
70
|
|
|
bitWriter = writer['write' + bitDepth + 'Bit' + (isFloat ? "Float" : "")]; |
71
|
|
|
} |
72
|
|
|
let i = 0; |
73
|
|
|
let j = 0; |
74
|
|
|
let len = numbers.length; |
75
|
|
|
let bytes = []; |
76
|
|
|
while (i < len) { |
77
|
|
|
j = bitWriter(bytes, numbers, i, j); |
78
|
|
|
i++; |
79
|
|
|
} |
80
|
|
|
if (isBigEndian) { |
81
|
|
|
endianness.endianness(bytes, bitDepths.bitDepthOffsets[bitDepth]); |
82
|
|
|
} |
83
|
|
|
return bytes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Turn bytes to base. |
88
|
|
|
* @param {!Array<string>|!Array<number>} bytes The bytes. |
89
|
|
|
* @param {number} base The base. |
90
|
|
|
* @param {Function} padFunction The function to use for padding. |
91
|
|
|
*/ |
92
|
|
|
function bytesToBase(bytes, base, padFunction=pad.padding) { |
93
|
|
|
if (base != 10) { |
94
|
|
|
let i = 0; |
95
|
|
|
let len = bytes.length; |
96
|
|
|
while (i < len) { |
97
|
|
|
bytes[i] = bytes[i].toString(base); |
98
|
|
|
padFunction(bytes, base, i); |
99
|
|
|
i++; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
module.exports.toBytes = toBytes; |
105
|
|
|
|