Hello, I'm too lazy to fork this repo, but I fixed your fromAscii and toAscii functions
Your from Ascii completely ignores the padding.
Your to Asciii returns weird, ugly characters at the end of the result.
fromAscii = function(str, padding) {
var hex = '0x';
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
var n = code.toString(16);
hex += n.length < 2 ? '0' + n : n;
}
return hex + '0'.repeat(padding*2 - hex.length + 2);
};
toAscii = function(hex) {
var str = '',
i = 0,
l = hex.length;
if (hex.substring(0, 2) === '0x') {
i = 2;
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) continue; // this is added
str += String.fromCharCode(code);
}
return str;
};
Hope this helps.
@debris can you please take a look at this?
Hei, just be wary that String.prototype.repeat is ES6::
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
Only available for the new versions of Firefox, Chrome, Safari and Node4.x and 5.x.
Note: This does not concern this pull request but rather the whole library.
I think those function should be split into "encoding / decoding" and "formatting for human consumption" parts.
It is not an option to remove trailing zeros for encoding/decoding (unless the type is shorter), but it is certainly nice to do that for humans.
Also please ban the function names "fromASCII" / "toASCII", since it is not only working on ASCII. Either use "hexToBinary" / "binaryToHex" or "hexToUTF8" / "hexFromUTF8".
I agree. @debris can you take care of this?, also when we introduce breaking changes like this lets follow the server standard and move to 1.0.0 (see #341 )
+1
I don't believe the proposed toAscii works correctly, neither the toUtf8 currently in the library. There are utf-8 characters that have longer hex representations than 2 numerals, like € for example. I suggest we do something with buffers like this:
var toUtf8 = function(hex) {
var buf = new Buffer(hex.replace('0x',''),'hex');
return buf.toString();
}
Just realized that my solution does not work for the hex representation of € I gave as example though...
I'm still monkey patching this, is this going to get into a release?
It seems like the latest version of web3 (0.15.3) converts hex into ascii ok, it only adds a padding of \u0000;
So I am using web3.toAscii(whatever).replace(/\u0000/g, '') ; This works;
@croqaz that makes it easy to monkey-patch:
// pseudo code, didn't actually attempt to run this yet
web3.toAsciiOriginal = web3.toAscii;
web3.toAscii = function (input) { web3.toAsciiOriginal(input).replace(/\u0000/g, '') }
However, this needs to be tested with UTF8, because per @chriseth above it should really be web3.toUTF8
What is the most up to date monkey patch ? I have a very long decimal number kept in bytes32 something that looks like this: 52.981828190778465. toUTF8 doesn't seem to convert the values correctly.
@croqaz is your last path working all good ?
Just hit this bug with fromAscii ignoring the padding. Is this going to be fixed?
Can you please test agains the web3 1.0 functions? https://github.com/ethereum/web3.js/blob/1.0/packages/web3-utils/src/utils.js#L181
Or give me something reproducible: what you put in and what you expect out.
@barkthins just forgotten the return
web3.toAsciiOriginal = web3.toAscii;
web3.toAscii = function (input) { return web3.toAsciiOriginal(input).replace(/\u0000/g, '') }
I hope those issues are a thing of the past in web3.js 1.0.0. Can somebody test this? on the utils from 1.0?
Hey y'all, just trying out the 1.0.0 beta here, and I'm runnin'
this.decodedHash = this.web3.utils.toAscii(this.receivedHash.toString());
even tried the iteration of
.replace(/\u0000/g, '')
but it seems to be that I still have this unicode â in place of quotes.
pre-beta and on Geth I'd receive:
â\u0080\u0099
so, there's some bit of an improvement it seems
are you guys waiting for a PR for this? it has been a while just to add padding as an argument to fromAscii function. shall I tackle this with the OP's comment?
Given from the comments, you implemented other functions to do this like hexToUtf8 for v1 but the docs for v0.2x still show the padding documented: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3fromascii
You might say to update to v1 however truffle comes with ^0.2x so I'd prefer as little intervention on the setup as possible.
I believe the correct way is to update the docs for 0.2x and close this issue because its mind buggling.
Version 0.20.x got his last maintenance release with v0.20.7. Please update your code to the latest 1.0 version of Web3.js. Further details about the current project state are explained in the release announcement of version 1.0.0-beta.38.
Most helpful comment
It seems like the latest version of web3 (0.15.3) converts hex into ascii ok, it only adds a padding of \u0000;
So I am using
web3.toAscii(whatever).replace(/\u0000/g, ''); This works;