Merge pull request #292 from caktux/develop

fix fromAscii, toAscii and toHex, fix wrong test assertions, add raw …
This commit is contained in:
caktux 2015-08-10 23:42:54 -04:00
commit 29d11b96de
4 changed files with 31 additions and 35 deletions

View File

@ -128,9 +128,6 @@ var toAscii = function(hex) {
} }
for (; i < l; i+=2) { for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16); var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code); str += String.fromCharCode(code);
} }
@ -168,9 +165,6 @@ var fromAscii = function(str) {
var hex = ""; var hex = "";
for(var i = 0; i < str.length; i++) { for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i); var code = str.charCodeAt(i);
if (code === 0) {
break;
}
var n = code.toString(16); var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n; hex += n.length < 2 ? '0' + n : n;
} }
@ -266,7 +260,7 @@ var toHex = function (val) {
else if(val.indexOf('0x') === 0) else if(val.indexOf('0x') === 0)
return val; return val;
else if (!isFinite(val)) else if (!isFinite(val))
return fromUtf8(val); return fromAscii(val);
} }
return fromDecimal(val); return fromDecimal(val);
@ -531,4 +525,3 @@ module.exports = {
isArray: isArray, isArray: isArray,
isJson: isJson isJson: isJson
}; };

View File

@ -5,7 +5,9 @@ var assert = chai.assert;
var tests = [ var tests = [
{ value: 'myString', expected: '0x6d79537472696e67'}, { value: 'myString', expected: '0x6d79537472696e67'},
{ value: 'myString\x00', expected: '0x6d79537472696e67'}, { value: 'myString\x00', expected: '0x6d79537472696e6700'},
{ value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -17,4 +19,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });

View File

@ -5,7 +5,9 @@ var assert = chai.assert;
var tests = [ var tests = [
{ value: '0x6d79537472696e67', expected: 'myString'}, { value: '0x6d79537472696e67', expected: 'myString'},
{ value: '0x6d79537472696e6700', expected: 'myString'}, { value: '0x6d79537472696e6700', expected: 'myString\u0000'},
{ value: "0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c",
expected: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -17,4 +19,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });

View File

@ -30,7 +30,9 @@ var tests = [
{ value: 'myString', expected: '0x6d79537472696e67'}, { value: 'myString', expected: '0x6d79537472696e67'},
{ value: new BigNumber(15), expected: '0xf'}, { value: new BigNumber(15), expected: '0xf'},
{ value: true, expected: '0x1'}, { value: true, expected: '0x1'},
{ value: false, expected: '0x0'} { value: false, expected: '0x0'},
{ value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ',
expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'}
]; ];
describe('lib/utils/utils', function () { describe('lib/utils/utils', function () {
@ -42,4 +44,3 @@ describe('lib/utils/utils', function () {
}); });
}); });
}); });