Can anybody give me a little advice please?
I have a string, for example "01001011" and what I need to do is to reverse it, so I used .split('')
than .reverse()
and now I need to read the array as a string and convert it to integer. Is it possible?
Thanks
How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.
To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .
parseInt() The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.
If you want to convert the array back to a string use join()
(MDN) and for converting a string to an integer use parseInt()
(MDN). The second argument of the later is an optional radix.
JavaScript will try to determine, what radix to use, but to be sure you should always add your radix manually. Citing from MDN:
If radix is undefined or 0, JavaScript assumes the following:
If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
If the input string begins with any other value, the radix is 10 (decimal).
So in your case the following code should work:
var a = '01001011'; var b = parseInt( a.split('').reverse().join(''), 2 );
or just (if you would want to convert the starting string, without the reversal):
var b = parseInt( a, 2 );
Just call parseInt
with a different radix, in this case use 2 for binary.
var a = parseInt("01001011", 2);
// a === 75
parseInt
attempts to figure out the radix itself when you don't explicitly specify it. From the Mozilla Developer Network:
If radix is
undefined
or 0, JavaScript assumes the following:
- If the input
string
begins with "0x" or "0X", radix is 16 (hexadecimal).- If the input
string
begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when usingparseInt
.- If the input
string
begins with any other value, the radix is 10 (decimal).
In this case, it is crucial that you do specify the radix, as otherwise it may be interpreted as either a decimal or an octal number. As a rule of thumb, always specify the radix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With