0

I need to efficiently convert the code of a capital char to its correspondent code in lower case. ie: Convert the ascii code 97 to 65. Corresponding to "A" and "a"

  • An wrong answer may be just to add or sustract 32 to that integer. This would only work for an standard English keyboard.

    Consider accented, non-English, double-byte, etc chars ie: Ñ=165 / ñ=164 , Ç=128 / ç=135

  • so far I am achieving it with this long detour:

    var myCapitalCode=65;
    var myLowerCode = String.fromCharCode(myCapitalCode).toLowerCase().charCodeAt()
    
Jongware
  • 22,200
  • 8
  • 54
  • 100
plmk
  • 2,194
  • 1
  • 15
  • 21
  • 4
    Sounds like you already found your answer. What's the problem now? – Bergi Oct 27 '15 at 11:47
  • Parsing it from ascii to char and from char to ascii again looks not very efficient. I believe there must be a better way to do it. – plmk Oct 27 '15 at 11:53
  • It's straightforward. It's exactly what you meant to do: convert code to character, lowercase it, convert to code. The code is clear and functional. – Bergi Oct 27 '15 at 11:58
  • 1
    "*Looks not very efficient*" - have you measured it? Do you have [actual performance problems](https://en.wikipedia.org/wiki/Premature_optimisation)? The only alternative I see was to download the Unicode character database, extract lowercase mappings from it, and store the charcodes in a huge lookup table (array). – Bergi Oct 27 '15 at 12:00
  • thanks Bergi. Maybe is beginners insecurity I just thought I wasnt able to find an existing function. – plmk Oct 27 '15 at 12:05
  • There *is* no existing function indeed. JS does not have an extra character datatype, only strings - characters are just strings of length 1. Lowercasing is intrinsically a string operation, not one on numbers (char codes). – Bergi Oct 27 '15 at 12:10

0 Answers0