How to convert letters to numbers with Javascript?
You can get a codepoint* from any index in a string using String.prototype.charCodeAt
. If your string is a single character, you’ll want index 0
, and the code for a
is 97 (easily obtained from JavaScript as 'a'.charCodeAt(0)
), so you can just do:
s.charCodeAt(0) - 97
And in case you wanted to go the other way around, String.fromCharCode
takes Unicode codepoints* and returns a string.
String.fromCharCode(97 + n)
* not quite
Convert letters to numbers
thanks for all the ideas, but I am a dumdum.
Here's what I did. Made a mapping from each letter to a specific number, then called each letter
df=data.frame(L=letters[1:26],N=rnorm(26))
df[df$L=='e',2]
Converting Letters to Numbers in C
If you need to deal with upper-case and lower-case then you may want to do something like:
if (letter >= 'A' && letter <= 'Z')
num = letter - 'A';
else if (letter >= 'a' && letter <= 'z')
num = letter - 'a';
If you want to display these, then you will want to convert the number into an ascii value by adding a '0' to it:
asciinumber = num + '0';
Related Topics
Assign Unique Id Based on Two Columns
How to Escape a Backslash in R
How to Generate a Matrix of Combinations
Different Size Facets Proportional of X Axis on Ggplot 2 R
How to Get Name of Variable in R (Substitute)
Moving Average of Previous Three Values in R
R - Use Rbind on Multiple Variables with Similar Names
Why Is Using Update on a Lm Inside a Grouped Data.Table Losing Its Model Data
How to Use the Strsplit Function with a Period
Select Values from Different Columns Based on a Variable Containing Column Names
Split a Column of Concatenated Comma-Delimited Data and Recode Output as Factors
Rolling Join on Data.Table with Duplicate Keys
Using Cut and Quartile to Generate Breaks in R Function
Add New Row to Dataframe, at Specific Row-Index, Not Appended
Combine Rows in Data Frame Containing Na to Make Complete Row