View Full Version : Adding characters - not concatenating
04-01-2003, 08:55 PM
Is there a way to convert back and forth from the ordinal value of a character to get a new value instead of the default concatination? In other words:
'a' + 5 = 'f'
not 'a' + 5 = 'a5'
04-01-2003, 09:05 PM
04-01-2003, 09:11 PM
Thanks liorean! That's it!
04-01-2003, 09:27 PM
i was thinking math not characters :o :) Ive deleted it anyway. Cheers
04-01-2003, 10:38 PM
String.prototype.hexAdd = function()
var arg, i = 0, sum = parseInt( this, 16 );
while( arg = parseInt( arguments[i++], 16 ) )
sum += arg;
return sum.toString( 16 );
var a = 'a';
alert( a.hexAdd( 5 ) );
var b = 4;
alert( b.toString().hexAdd( '1f', 5, 'c2' ) );So, to directly answer your question, yes. parseInt takes a 2nd argument, the radix. toString takes a radix argument as well ;)
04-01-2003, 11:29 PM
How does parseInt handle radices different from 2...10,12,16? (Those that are even slightly probably to occur...)
For example, how does it handle 40? (or any other value higher than the amount of numbers and letters total in English language?)
04-01-2003, 11:45 PM
If the passed value i not part of the target radix, then NaN is returned, such as
parseInt( 'f', 2 );
04-02-2003, 12:01 AM
i believe radices higher than 36 either return an error, or are ignored, and treated as 10
04-02-2003, 12:03 AM
That was given - but read this: "For radixes above ten, the letters of the alphabet indicate numerals greater than nine." How does it handle radices when the radix is greater than what /0-9a-z/ covers?
04-02-2003, 12:07 AM
Oh, I see what you mean. Just returns NaN. :p
04-02-2003, 12:12 AM
To answer that question (at least in mozilla's implementation), radix can only be 2...36 - other values always return NaN. toString with a radix above 36 gives this error, though:
Error: illegal radix 37
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.