Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding characters - not concatenating

    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'
    Brad Eck
    www.sitesdynamic.com

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    var str=String.fromCharCode(5+('a').charCodeAt());
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean! That's it!

    THANK YOU!!!

    Brad
    Last edited by bradeck; 04-01-2003 at 08:57 PM.
    Brad Eck
    www.sitesdynamic.com

  • #4
    Regular Coder
    Join Date
    Mar 2003
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i was thinking math not characters Ive deleted it anyway. Cheers

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    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
    Last edited by beetle; 04-01-2003 at 09:43 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the passed value i not part of the target radix, then NaN is returned, such as

    parseInt( 'f', 2 );

    http://devedge.netscape.com/library/...s.html#1008379
    Last edited by beetle; 04-01-2003 at 10:55 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #8
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i believe radices higher than 36 either return an error, or are ignored, and treated as 10
    Last edited by joh6nn; 04-01-2003 at 11:24 PM.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I see what you mean. Just returns NaN.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •