web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

web3.fromAscii and web3.toAscii

Open croqaz opened this issue 11 years ago • 18 comments

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.

croqaz avatar Oct 29 '15 16:10 croqaz

@debris can you please take a look at this?

frozeman avatar Oct 29 '15 17:10 frozeman

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.

croqaz avatar Oct 30 '15 09:10 croqaz

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".

chriseth avatar Oct 31 '15 15:10 chriseth

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 )

frozeman avatar Nov 02 '15 09:11 frozeman

+1

aakilfernandes avatar Feb 24 '16 19:02 aakilfernandes

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();
}

MrChico avatar Mar 04 '16 22:03 MrChico

Just realized that my solution does not work for the hex representation of € I gave as example though...

MrChico avatar Mar 04 '16 22:03 MrChico

I'm still monkey patching this, is this going to get into a release?

barkthins avatar Mar 16 '16 16:03 barkthins

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 avatar Mar 17 '16 08:03 croqaz

@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

barkthins avatar Mar 18 '16 20:03 barkthins

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 ?

0xtuytuy avatar Mar 19 '17 20:03 0xtuytuy

Just hit this bug with fromAscii ignoring the padding. Is this going to be fixed?

zabesto avatar May 16 '17 05:05 zabesto

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.

frozeman avatar May 16 '17 08:05 frozeman

@barkthins just forgotten the return

web3.toAsciiOriginal = web3.toAscii;
web3.toAscii = function (input) { return web3.toAsciiOriginal(input).replace(/\u0000/g, '') }

danielfebrero avatar Jul 06 '17 09:07 danielfebrero

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?

frozeman avatar Aug 08 '17 15:08 frozeman

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

JamesKnippel avatar Sep 01 '17 03:09 JamesKnippel

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.

nynevi avatar Apr 02 '18 19:04 nynevi

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.

nivida avatar Mar 25 '19 19:03 nivida