Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional padding argument for fromUtf8 and fromAscii functions #375

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/utils/utils.js
Expand Up @@ -144,7 +144,10 @@ var toAscii = function(hex) {
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
var fromUtf8 = function(str) {
var fromUtf8 = function(str, pad) {

pad = pad === undefined ? 0 : pad;

str = utf8.encode(str);
var hex = "";
for(var i = 0; i < str.length; i++) {
Expand All @@ -155,6 +158,10 @@ var fromUtf8 = function(str) {
hex += n.length < 2 ? '0' + n : n;
}

while (hex.length < pad*2){
hex += "00";
}

return "0x" + hex;
};

Expand All @@ -166,14 +173,21 @@ var fromUtf8 = function(str) {
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
var fromAscii = function(str) {
var fromAscii = function(str, pad) {

pad = pad === undefined ? 0 : pad;

var hex = "";
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;
}

while (hex.length < pad*2){
hex += "00";
}

return "0x" + hex;
};

Expand Down