View Full Version : Adding characters - not concatenating

04-01-2003, 07: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, 08:05 PM
var str=String.fromCharCode(5+('a').charCodeAt());

04-01-2003, 08:11 PM
Thanks liorean! That's it!



04-01-2003, 08:27 PM
i was thinking math not characters :o :) Ive deleted it anyway. Cheers

04-01-2003, 09: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, 10: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, 10:45 PM
If the passed value i not part of the target radix, then NaN is returned, such as

parseInt( 'f', 2 );


04-01-2003, 11:01 PM
i believe radices higher than 36 either return an error, or are ignored, and treated as 10

04-01-2003, 11:03 PM
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-01-2003, 11:07 PM
Oh, I see what you mean. Just returns NaN. :p

04-01-2003, 11:12 PM
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
Source File: javascript: (100).toString(37);
Line: 1

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum