Convert Numbers to FULL binary octets [duplicate] - javascript

This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 6 years ago.
Take this IP address:
192.168.1.1
I want to break it down into 4 full binary octets, so:
11000000 . 10101000 . 00000001 . 00000001.
All of the conversions I know, and those that I've found on other stackoverflow questions, only return the binary number itself, for example:
(1 >>> 0).toString(2) returns 1 when I want 00000001
Number(2).toString(2) returns 10 when I want 00000010
Is there an in-built javascript method that I haven't come across yet or do I need to manually add the 0's before depending on the number?

You can use Number#toString method with radix 2 for converting a Number to corresponding binary String.
var str = '192.168.1.1';
console.log(
// Split strings based on delimiter .
str.split('.')
// iterate over them
.map(function(v) {
// parse the String and convert the number to corresponding
// binary string afterwards add preceding 0's
// with help of slice method
return ('00000000' + Number(v).toString(2)).slice(-8)
// rejoin the string
}).join('.')
)

The simplest solution is to prefix the answer with zeros and then use a negative substring value to count from the right, not the left...
alert(("00000000" + (1 >>> 0).toString(2)).substr(-8));

Related

How do I convert an integer in JavaScript to a 32bits? [duplicate]

This question already has answers here:
How do you convert numbers between different bases in JavaScript?
(21 answers)
Closed 5 months ago.
I have some numbers (base 10) that I want to convert to a 32 bits(base 2), I have tried a lot of things, I found out the >>> operator, but apparently it only converts negative numbers to a base 10 the equivalent of the 32 bits, instead of base 2
const number = 3
const bitNumber = 1 >>> 0
console.log(bitNumber) /// 1
The numbers are always stored as bits internally. It’s console.log that converts them to a string, and the string conversion uses decimal by default.
You can pass a base to Number.prototype.toString:
console.log(bitNumber.toString(2));
and display as many bit positions as you want:
console.log(bitNumber.toString(2).padStart(32, '0'));

convert string representation of float to number - keep decimals the same [duplicate]

This question already has an answer here:
Keep trailing or leading zeroes on number
(1 answer)
Closed 9 months ago.
I have some string values like "35.5" , "32.20" and I want them to be converted to numbers but keep the exact same decimals. When I use Number("32.0") for example I get 32 but I want 32.0. If I convert Number("35.5") I want 35.5 not 35.50, is there any way to do this easily?
if you want a fixed number of floating point you can use toFixed but be aware that returns a string
const strings = [ "35.5" , "32.20", "32.0"]
const result = strings.map(n => parseFloat(n).toFixed(1))
console.log(result)

How to Convert Double to Hexadecimal using javascript with 0x [duplicate]

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 2 years ago.
I have a variable like this:
var currency = "4,990.17"
currency.replace(/[$,]+/g,"");
var currency2 = parseDouble(currency)-0.1;
How can I set currency2 to be hexadecimal with 0x in front of it?
I would like my string hexadecimal value of 4999.17 to become:
0x137E.2B851EB851EB851EB852
Once converted to a number you can call .toString([radix]) ( MDN Docs ) with an optional radix which is in the range 2 through 36 specifying the base to use for representing numeric values.
var currency = 4990.17;
hexCurrency = currency.toString(16);
console.log(hexCurrency);
This returns 137E.2B851EB851EB851EB852 or you can add 0x by doing
hexCurrency = "0x" + currency.toString(16);

Addition of two strings gives unintended result [duplicate]

This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 3 years ago.
I would like to get a numeric value in a string which is in the format 1 111.
I used a regex to extract it:
([0-9]*)\s([0-9]*)
then I thought that I will obtain the correct result with this operation:
regex_result[1]*1000+regex_result[2]
But actually I just have to addition them and I do not understand why.
var str= "Bit rate : 5 333 kb/s"
var bitrate= str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate);
//attempted result, but incorrect code
console.log(bitrate[1]+bitrate[2]);
//attempted correct code, but wrong result
console.log(bitrate[1]*1000+bitrate[2]);
Here, the second captured group just so happens to be 3 characters long, so multiplying the first captured group by 1000 and adding it to the second group will just so happen to produce the same result as plain concatenation.
But you have to add them together properly first. Your second attempt isn't working properly because the right-hand side of the + is a string, and a number + a string results in concatenation, not addition:
var str = "Bit rate : 5 333 kb/s"
var bitrate = str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate[1] * 1000 + Number(bitrate[2]));
If the input isn't guaranteed to have exactly 3 digits in the second capturing group, the concatenation method won't work.
You can parse them as ints instead of manipulating strings
var str= "Bit rate : 5 333 kb/s"
var bitrate= str.match(/Bit\srate\s*:\s([0-9]*)\s([0-9]*)\s/);
console.log(bitrate);
console.log(parseInt(bitrate[1] * 1000) + parseInt(bitrate[2]));

Convert Large Decimal Number to String in Javascript [duplicate]

This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 5 years ago.
I have large decimal numbers which I am getting from a request & I want to convert them to string.
So for EG:
I tried all methods converting to string
var r=12311241412412.1241523523523235
r.toString();
r+'';
''+r;
String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'
All methods return the decimal numbers upto 4 digits (12311241412412.1241)
but i want all the number till end.
I also tried r.toFixed().toString() but each time the length of decimal numbers change.
What would be easy way to do this?
the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.
use this instead:
var r = "12311241412412.1241523523523235";

Categories