Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .toString(16) always return lowercase?

When converting a decimal number to a base above 10 using .toString(base), it seems that I always get a lower case string. Can I rely on this? An upper case string would be correct, although would need converting for my application.

Extra credit for referencing the part of the spec that defines this (I looked and couldn't find it) and for any counter-examples (browsers that return uppercase).

Example:

(12648430).toString(16) // returns: "c0ffee". Not "C0FFEE"
like image 981
ColBeseder Avatar asked Nov 03 '13 10:11

ColBeseder


People also ask

How to lowercase the string returned by ToString method?

If the String object returned by the ToString (IFormatProvider) method is to be written to an XML file, its String.ToLowerInvariant method should be called first to convert it to lowercase. Thanks for contributing an answer to Stack Overflow!

Does the toString () method change the original string?

The toString () method does not change the original string. The toString () method can be used to convert a string object into a string. Every JavaScript object has a toString () method.

How to convert a string to lowercase in Java?

The toLowerCase () method converts a string to lowercase letters. The toLowerCase () method does not change the original string. The string converted to lowercase.

What is the return type of [toString () ]?

[ToString. ()] returns the constants "True" or "False". Note that XML is case-sensitive, and that the XML specification recognizes "true" and "false" as the valid set of Boolean values.


2 Answers

Yes, it's always lower case. That's been defined in the specification since the 5th edition in 2009. Here's what it says in the 5.1 spec; 5.0 isn't directly linkable, but it said much the same thing:

If ToInteger(radix) is not an integer between 2 and 36 inclusive throw a RangeError exception. If ToInteger(radix) is an integer from 2 to 36, but not 10, the result is a String representation of this Number value using the specified radix. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-dependent if the radix is not 10, however the algorithm should be a generalisation of that specified in 9.8.1.

(my emphasis)

The current spec continues to specify using a-z (in lower case).

Previously, the 3rd edition spec (there was no 4th edition) from 1999 did not say that, it just said:

If radix is an integer from 2 to 36, but not 10, the result is a string, the choice of which is implementation-dependent.

...but now that browsers that were created when the 3rd edition spec was the current spec are well and truly gone, you can rely on it being in lower case. And indeed, you probably could even when this answer was first posted in 2013, since they didn't usually add that kind of thing to the specification if there were significant known implementations that did something different.

Just for fun, here's a quick check:

const str = (12648430).toString(16);
console.log(`${str} === c0ffee? ${str === "c0ffee"}`);
like image 153
T.J. Crowder Avatar answered Oct 24 '22 06:10

T.J. Crowder


(12648430).toString(16) will always return: "c0ffee". Not "C0FFEE", after checking it with somes browsers, I found a confirmation:

The Number object overrides the toString() method of the Object object; it does not
inherit Object.prototype.toString(). For Number objects, the toString() method returns a string representation of the object in the specified radix.

The toString() method parses its first argument, and attempts to return a string
representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), a through f are used.

"for hexadecimal numbers (base 16), a through f are used".

See reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString .

like image 1
jacouh Avatar answered Oct 24 '22 05:10

jacouh