简体   繁体   中英

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"

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 az 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 az (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"}`);

(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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM