How do I convert a ASCII hex string (ex. "6C" to "l") in javascipt? [duplicate] - javascript

This question already has answers here:
Hex Number to Char using Javascript
(2 answers)
Closed 16 hours ago.
How do I convert an ASCII hex string into a character?
Examples:
6E - n
26 - &
45 - E
I have searched on the internet but only found answers for the decimal system, not the double-hex format.

Use a combination of parseInt() and String.fromCharCode():
const hex2Char = hex => String.fromCharCode(parseInt(hex, 16));
console.log(hex2Char('6E'));
console.log(hex2Char('26'));
console.log(hex2Char('45'));

Related

how to put a divider char between a number string in javascript for example I have 123456 and I want to be 123,456 or 1000000 to 1,000,000 [duplicate]

This question already has answers here:
How to format numbers? [duplicate]
(17 answers)
Closed 2 years ago.
I want to put a price on something and I get a string number and it should be divided by every 3 letters
but what eve I try to do I can't
is there any function or way that could be helpful ?
The simplest way is using Number#toLocaleString with a locale. The locale en-US uses commas to delimit every third digit and a dot to delimit the decimal fractional part.
const n = 1000000
const fractional = 12345.67
console.log(n.toLocaleString('en-US')) // 1,000,000
console.log(fractional.toLocaleString('en-US')) // 12,345.67
Use this function:
function numWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
console.log(numWithCommas(100000));

How to encode 256 bit binary string as base64? [duplicate]

This question already has answers here:
Convert binary data to base64 with javascript
(7 answers)
Closed 3 years ago.
I have 256 character string each character is 1 or 0 and I need to convert it to base64 how to do this?
var your256bitstring;
// Base 64 encoded
var b64 = btoa(your256bitstring);
// :)

Converting from number to string gives strange number in Javascript [duplicate]

This question already has answers here:
How do I work around JavaScript's parseInt octal behavior?
(10 answers)
Closed 8 years ago.
So I saw this strange scenario. I wanted to convert a number to a String in Node.js and I got the following.
01010100132.toString()
Turns into
"136347738"
Can someone explain this to me?
Any numeric constant prefixed with a 0 is an octal literal (assuming all its digits are valid octal digits).
var i = 010; // 8 decimal

javascript parseInt errors with long hex string [duplicate]

This question already has answers here:
parseInt rounds incorrectly
(2 answers)
Closed 8 years ago.
I am trying to parse a hex string to a decimal number in javascript, but i meet something so weird.
The hex string is '27a4b0795a7d749c', I know the decimal number is 2856602098915439772 checked by python and windows's calc. But the js's parseInt doesn't return right answer.
Here's the test code:
var hex = '27a4b0795a7d749c';
console.log(hex);
var num = parseInt(hex, 16);
console.log(num);
var hex2 = num.toString(16);
console.log(hex2);
the console display (I am using Chrome 30.0.1599.101 m on Windows 64bits):
I test it in IE9, and it get 2856602098915439600 as well. But how does this happened?
Per this answer (which cites ECMA specs) the largest Javascript integer is 9007199254740992 - way less than your expected value.

Formatting Hex in Javascript [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
How to output numbers with leading zeros in JavaScript? [duplicate]
(6 answers)
Closed 8 years ago.
How can I format a hex to be displayed always with 4 digits in javascript?
For example, I converting a decimal to hex :
port = 23
function d2h(d) {return (+d).toString(16);}
d2h(port)
I am successfully able to convert "23" to the hex value "17". However, I would like to be formatted like this "0017" (with four digits -- appending 0's before the 17).
All information would be greatly appreciated.
Here is an easy way:
return ("0000" + (+d).toString(16)).substr(-4);
Or:
return ("0000" + (+d).toString(16)).slice(-4);
You can use sprintf
http://www.diveintojavascript.com/projects/javascript-sprintf
I havent tried it yet but something like this should work:
sprintf("%.4X", d)

Categories

Resources