Reply LinkBack Thread Tools Search this Thread
Old 28-01-2008, 06:55   #1 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
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.
__________________
  Reply With Quote
Old 28-01-2008, 07:18   #2 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 192
Send a message via AIM to Cborrow
You can use getCharAt(index) to return ASCII Codes for characters.

Code:
var myString = 'Text'; myString.getCharAt(0);

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.
__________________
  Reply With Quote
Old 28-01-2008, 08:33   #3 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,377
Is there a good reason why the database won't take alphanumeric codes?
  Reply With Quote
Old 28-01-2008, 11:22   #4 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
Yes.
__________________
  Reply With Quote
Old 28-01-2008, 11:49   #5 (permalink)
haku
shiro
 
haku's Avatar
 
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.
  Reply With Quote
Old 28-01-2008, 15:39   #6 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
Quote:
Originally Posted by haku
Never seen a database yet (granted I havent seen many) that doesn't accept alphanumeric codes.

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.
__________________
  Reply With Quote
Old 28-01-2008, 16:44   #7 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 192
Send a message via AIM to Cborrow
Any chance of setting up a separate table for storing data and use that field for storing the ID of the row in the separate table?

Other then that and storing the data outside of a database I can't think of anything.
__________________
  Reply With Quote
Old 28-01-2008, 21:53   #8 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,659
Quote:
Seriously - Why are there so many fucking no it alls, in here.

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.
  Reply With Quote
Old 29-01-2008, 05:38   #9 (permalink)
MikeMackay
Everything is fine.
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Witham & London
Posts: 723
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Quote:
Originally Posted by haku
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.
This is a great point, how would your system know where the division is ? Without knowing a little more about your exact requirements, and possibly with a few examples, I'm not sure if anyone can help?

- Mike
  Reply With Quote
Old 29-01-2008, 12:31   #10 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
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.
  Reply With Quote
Old 29-01-2008, 14:35   #11 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
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.
__________________
  Reply With Quote
Old 29-01-2008, 15:23   #12 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
Join Date: Sep 2006
Posts: 851
did that ^ make any sense then ?
__________________
meh.
  Reply With Quote
Old 29-01-2008, 15:54   #13 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
Yeah. Should do the bit hopefully. Cheers.
__________________
  Reply With Quote
Old 29-01-2008, 21:15   #14 (permalink)
hobolooter
Registered User
 
hobolooter's Avatar
 
Join Date: Feb 2004
Location: USA
Posts: 52
Send a message via AIM to hobolooter
haha since when has paulanthony not been a dick?

more seriously though, did you get it figured out, or do you need other suggestions, i might have a few crazy ideas, but they would all involve something utterly insane.
  Reply With Quote
Old 30-01-2008, 04:44   #15 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
Join Date: Sep 2006
Posts: 851
fuck off troll
__________________
meh.
  Reply With Quote
Old 30-01-2008, 07:48   #16 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
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:
/////////////////////////////////////////////////////////////////////////////// // // methods to [de]marshal alphanumeric [<]=> numeric string // // A-Z !a-z - if you need lcase you'll need another leading 0 et al... // ...you're already doubling up on your storage doing this // // all methods take & return a string // pad also takes the desired length // function pad(s,l){var p=s;var i=l-s.length;do{p='0'+s;}while(i--);return p;} function marshal(s){ var m=''; var i=s.length-1; do{var c=s[i];m=(c.match(/[0-9]/)?'0'+c:c.charCodeAt(0))+m;}while(i--); return m; } function demarshal(s){ var d=''; for (var i=s.length;i>-1;i-=2){ var c=s[i]+s[i+1]; d=(c[0]==0?c[1]:String.fromCharCode(c))+d; } return d; } // test alert( marshal('12AB') ); //-> 01026566 alert( demarshal( pad('1026566', 8) ) ); //-> 12AB alert( marshal('FOOBAR69') ); //-> 7079796665820609 alert( demarshal('7079796665820609') ); //-> FOOBAR69 // // ///////////////////////////////////////////////////////////////////////////////
__________________
meh.

Last edited by proc355 : 30-01-2008 at 09:09.
  Reply With Quote
Old 31-01-2008, 03:16   #17 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,028
Send a message via MSN to paulanthony
Kudos my friend. that will be useful.


I'd been working on my own functions, but they weren't optimised, and this code seems alot tidier. ta.
__________________
  Reply With Quote
Old 31-01-2008, 03:29   #18 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
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.
  Reply With Quote
Old 31-01-2008, 06:19   #19 (permalink)
haku
shiro
 
haku's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 1,659
Nice little piece of code Proc.
  Reply With Quote
Old 03-02-2008, 09:34   #20 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
Join Date: Sep 2006
Posts: 851
Quote:
Originally Posted by haku
Nice little piece of code Proc.
cheers

even tighter…
Code:
<html> <head> <script type="text/javascript"> function pad(s,l){var p=s,i=l-s.length;do{p="0"+s;}while(i--);return p;} function marshal(s){ var m="",i=s.length-1; do{var c=s[i];m=(c.match(/[0-9]/)?"0"+c:c.charCodeAt(0))+m;}while(i--); return m; } function demarshal(s){ var d="",i=s.length; do{var c=s[i-2]+s[i-1];d=(c[0]==0?c[1]:String.fromCharCode(c))+d;}while(i-=2); return d; } function testme(){ alert(marshal("12AB")); alert(demarshal(pad("1026566", 8))); alert(marshal("FOOBAR69")); alert(demarshal("7079796665820609")); } </script> </head> <body onload="testme()" /> </html>

obviously counting to 2 was beyond me the other morning.
__________________
meh.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8