| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
mingin dawg baitch
|
Javascript Letters to Numeric?
Guys if I have a string such as 2WAY002 or 2WAY001 What is the best way to get rid of the characters in the middle of the string and convert them to numbers in Javascript. My initial thoughts was a function to get the position in the alphabet i.e. A=1 and change to a number. Is there any better way? would parseInt do anything for me? or a regex? My reason for doing this is that these codes are going into a database from a CSV file...and need to be numeric. I wish to run a javascript parser over the file first and modify the data. |
|
|
|
|
|
#2 (permalink) |
|
I like code.
|
You can use getCharAt(index) to return ASCII Codes for characters. Code:
That would give you the ASCII code for T you can then loop through the string based on string length and convert each character to its ASCII value. Also I believe parseInt would just return 2. |
|
|
|
#5 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,659
|
Never seen a database yet (granted I havent seen many) that doesn't accept alphanumeric codes. going from A => 1, B => 2 etc will work great up to 'i', but once you hit 'j' you are in the double digit letters, which depending on your reasoning for converting to numbers could screw everything up. You definitely wont be able to convert back without some indicator of where the division between letters is. |
|
|
|
#6 (permalink) | |
|
mingin dawg baitch
|
Quote:
Neither have I. But that the requirement. Seriously - Why are there so many fucking no it alls, in here. The reason I can't use alphanumeric codes is because that column is part of an existing system. There is no scope for ammending this column. OK? Good. |
|
|
|
|
#8 (permalink) | |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,659
|
Quote:
Its not that there are so many know it alls, rather there are a whole lot of dont-know-enough-but-think-they-dos that are trying to reach a solution that isn't a very good one. If they just give the reasons behind what they are going for, often the 'know-it-alls' can come up with a better, more effective and efficient solution. Now the answer to your problem is... oh wait, you're a dick. Figure it out yourself. |
|
|
|
|
#9 (permalink) | |
|
Everything is fine.
|
Quote:
- Mike |
|
|
|
|
#10 (permalink) |
|
Trailer Trash™
Join Date: Sep 2006
Posts: 851
|
last time i checked ascii A == 65 & a == 97… etc. 0-9 are the problem—zero-pad them; you're always dealing with two numbers for each position then e.g. 78AF01 => 070865700001 build it as a string then parseint it leading zero will probably be an issue if the field is indeed purely numeric; check the length on retrieval—if it's short add it/them back when before deserializing why the fuck you would want to do it in javascript is beyond me though, this is for your server-side language to deal with (which may have a serializer that does just this—although i've never come across any that serializes just to numeric [off to check :P]) *ok scratch the bit about why javascript, im too lazy to edit it out meh.
|
|
|
|
#11 (permalink) |
|
mingin dawg baitch
|
Its for part of a javascript parsing engine...I have a .NET program which processes CSV files. This maps different CSV structures to database fields..an XML file tells the field where to go. On top of that, a small Javascript parsing Engine is used within the code to change the format of the code (pad digits, replace digits etc)..Its not a web application, simply uses a javascript parser for flexibility. |
|
|
|
#16 (permalink) |
|
Trailer Trash™
Join Date: Sep 2006
Posts: 851
|
ok i proofed this last night—there isn't much in it—but have been fiddling around with it trying to squeeze every last drop of speed out of it/minimise the footprint (don't ask me why, it just piqued my interest & i haven't been playing with any actual javascript lately) anyway, can't seem to get better than this (golfed to death where line length is less than 80 chars) Code:
meh.
Last edited by proc355 : 30-01-2008 at 09:09. |
|
|
|
#18 (permalink) |
|
Trailer Trash™
Join Date: Sep 2006
Posts: 851
|
np i can't get the demarshal in the same form as the marshal—head scratcher, should be simple (might have just overlooked something) readable it aint but it is as (i believe) fast as you are going to get. meh.
|
|
|
|
#20 (permalink) | |
|
Trailer Trash™
Join Date: Sep 2006
Posts: 851
|
Quote:
even tighter… Code:
obviously counting to 2 was beyond me the other morning. meh.
|
|
|
![]() |