admin管理员组文章数量:1023596
Hey everyone quick question, I know this sounds strange to do in javascript but i have good use for it. I need to be able to parse a string passed in a textarea in such a way that escaped hex literals "\x41" or whatever are processed not as four chars '\' 'x' '4' '1' but as 'A' for example:
var anA = "\x41";
console.log(anA); //emits "A"
var stringToParse = $(#someTextArea).val(); //using jquery for ease not a req
//lets say that "someTextArea" contains "\x41"
console.log(stringToParse); // equals "\" "x" "4" "1" -- not what i want
console.log(new String(stringToParse)); same as last
console.log(""+stringToParse); still doesnt work
console.log(stringToParse.toString()); failz all over (same result)
I want to be able to have a way for stringToParse to contain "A" not "\x41"... any ideas beyond regex? I'll take a regex i guess, i just wanted a way to make javascript do my bidding :)
Hey everyone quick question, I know this sounds strange to do in javascript but i have good use for it. I need to be able to parse a string passed in a textarea in such a way that escaped hex literals "\x41" or whatever are processed not as four chars '\' 'x' '4' '1' but as 'A' for example:
var anA = "\x41";
console.log(anA); //emits "A"
var stringToParse = $(#someTextArea).val(); //using jquery for ease not a req
//lets say that "someTextArea" contains "\x41"
console.log(stringToParse); // equals "\" "x" "4" "1" -- not what i want
console.log(new String(stringToParse)); same as last
console.log(""+stringToParse); still doesnt work
console.log(stringToParse.toString()); failz all over (same result)
I want to be able to have a way for stringToParse to contain "A" not "\x41"... any ideas beyond regex? I'll take a regex i guess, i just wanted a way to make javascript do my bidding :)
Share Improve this question asked Apr 26, 2012 at 2:07 RyanRyan 2,82518 silver badges30 bronze badges 02 Answers
Reset to default 6String.prototype.parseHex = function(){
return this.replace(/\\x([a-fA-F0-9]{2})/g, function(a,b){
return String.fromCharCode(parseInt(b,16));
});
};
and in practice:
var v = $('#foo').val();
console.log(v);
console.log(v.parseHex());
I figured it out although its kind of hacky and i use eval :(... if anyone has a better way let me know:
stringToParse = stringToParse.toSource().replace("\\x", "\x");
stringToParse = eval(stringToParse);
console.log(stringToParse);
mainly i needed this to parse mixed strings... as in string literals with hex mixed in
Hey everyone quick question, I know this sounds strange to do in javascript but i have good use for it. I need to be able to parse a string passed in a textarea in such a way that escaped hex literals "\x41" or whatever are processed not as four chars '\' 'x' '4' '1' but as 'A' for example:
var anA = "\x41";
console.log(anA); //emits "A"
var stringToParse = $(#someTextArea).val(); //using jquery for ease not a req
//lets say that "someTextArea" contains "\x41"
console.log(stringToParse); // equals "\" "x" "4" "1" -- not what i want
console.log(new String(stringToParse)); same as last
console.log(""+stringToParse); still doesnt work
console.log(stringToParse.toString()); failz all over (same result)
I want to be able to have a way for stringToParse to contain "A" not "\x41"... any ideas beyond regex? I'll take a regex i guess, i just wanted a way to make javascript do my bidding :)
Hey everyone quick question, I know this sounds strange to do in javascript but i have good use for it. I need to be able to parse a string passed in a textarea in such a way that escaped hex literals "\x41" or whatever are processed not as four chars '\' 'x' '4' '1' but as 'A' for example:
var anA = "\x41";
console.log(anA); //emits "A"
var stringToParse = $(#someTextArea).val(); //using jquery for ease not a req
//lets say that "someTextArea" contains "\x41"
console.log(stringToParse); // equals "\" "x" "4" "1" -- not what i want
console.log(new String(stringToParse)); same as last
console.log(""+stringToParse); still doesnt work
console.log(stringToParse.toString()); failz all over (same result)
I want to be able to have a way for stringToParse to contain "A" not "\x41"... any ideas beyond regex? I'll take a regex i guess, i just wanted a way to make javascript do my bidding :)
Share Improve this question asked Apr 26, 2012 at 2:07 RyanRyan 2,82518 silver badges30 bronze badges 02 Answers
Reset to default 6String.prototype.parseHex = function(){
return this.replace(/\\x([a-fA-F0-9]{2})/g, function(a,b){
return String.fromCharCode(parseInt(b,16));
});
};
and in practice:
var v = $('#foo').val();
console.log(v);
console.log(v.parseHex());
I figured it out although its kind of hacky and i use eval :(... if anyone has a better way let me know:
stringToParse = stringToParse.toSource().replace("\\x", "\x");
stringToParse = eval(stringToParse);
console.log(stringToParse);
mainly i needed this to parse mixed strings... as in string literals with hex mixed in
本文标签: parsing hex literals in javascriptStack Overflow
版权声明:本文标题:parsing hex literals in javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745583596a2157454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论