forked from cconci/awesomeTerminalChrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringFxn.js
83 lines (54 loc) · 1.87 KB
/
stringFxn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
Awesome Terminal
cconci
*******************************************************************************/
function padStringLeft(stringToPad,NumberToPadFor,stringToPadWith) {
/*
js has not string padding fxn?????
*/
console.log(stringToPad.length +'\n');
while(stringToPad.length < NumberToPadFor) {
stringToPad = stringToPadWith+stringToPad;
}
return stringToPad;
}
function arrayAlementToString(arrayElement) {
return padStringLeft(arrayElement.toString(16),2,"0")+" ";
}
function arrayAlementsToString(arrayData) {
var output = "";
for(i=0;i<arrayData.byteLength;i++) {
output += padStringLeft(arrayData[i].toString(16),2,"0")+" "; //.toString(16); turns our int to a hex string
}
return output;
}
function hexStringToByteArray(hexString) {
/*
Convertes a string like 'AB 00 01 22 FF 10'
to a Byte array
var byteBuffer = new ArrayBuffer(9);
var byteBufferView = new Int8Array(byteBuffer);
byteBufferView[0] = 0xAB;
byteBufferView[1] = 0x00;
byteBufferView[2] = 0x01;
byteBufferView[3] = 0x22;
byteBufferView[4] = 0xFF;
byteBufferView[5] = 0x10;
byteBufferView[6] = '\n';
*/
//strip spaces
var hexStringTrim = hexString.trim();
//step one sepeate the string data, explode on spaces
var splitHexString = hexStringTrim.split(' ');
var byteBuffer = new ArrayBuffer(splitHexString.length);
//can not edit the ArrayBuffer, need to go through this method
var byteBufferView = new Int8Array(byteBuffer);
for(i=0;i<splitHexString.length;i++) {
//form each byte
//http://www.w3schools.com/jsref/jsref_parseint.asp
var byte = parseInt(splitHexString[i],'16'); //it is a hex string, thus 16
byteBufferView[i] = byte;
console.log(byte +'\n');
}
return byteBuffer;
}