Formatting Hex in Javascript [duplicate] - javascript

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)

Related

What can I change in my code to extract the first number? [duplicate]

This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 1 year ago.
I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -
If there are two separate numbers in the string - return the first of them.
So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:
function count(apples) {
const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
console.log(number);
}

How do I convert my HEX string into a number? [duplicate]

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?
You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax

Converting Array of Strings to floats (?!?!) [duplicate]

This question already has answers here:
Parsing numbers with a comma decimal separator in JavaScript
(3 answers)
Closed 3 years ago.
I have an array like this:
["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
and I would like to convert it to an array of numbers/floats.
I tried .map(Number) and also parseInt() and parseFloat() but didn't seem to work out.
Can anybody help?
You need to replace . by empty string and , by . before parsing it to float
let arr = ["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
let final = arr.map(v=> parseFloat(v.replace(/\./g,'').replace(',','.')))
console.log(final)

javascript parseInt on php [duplicate]

This question already has answers here:
How do I convert a string that looks like a hex number to an actual hex number in php?
(3 answers)
Closed 5 years ago.
Simply I need this function on PHP:
parseInt('0x0' + 'e');
It returns 14.
parseInt('0x0' + 'b');
It returns 11.
THIS IS A JAVASCIPT. NOW I WANT TO DO IT ON PHP.
I tried this but not returned 11 or 14.
$a[] = intval('0x0' . '14');
I got it from this link: https://www.experts-exchange.com/questions/21596911/ParseInt-function-for-PHP.html
I need to make my code like this:
intval(substr('e',0,8),16); // returns 14
this is what you need, remove quotes from 0x or hex numbers
echo intval(0xe);
// => 14

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

Categories

Resources