Aphanumeric Counter...

1. Junior Member
Join Date
Dec 1969
Posts
12

## Aphanumeric Counter...

I have a problem trying to solve this...<BR>I need a function to convert a given number to an alphanumeric number like this:<BR><BR>999 = AA999<BR>1035 = AB035<BR>26999 = AZ999<BR>27000 = BA000<BR> :<BR> :<BR> :<BR>max alphanueric number = ZZ999<BR><BR>It looks easy but it isn&#039;t<BR>This is a good exercise for all...<BR><BR>Any help are welcome....

2. Senior Member
Join Date
Dec 1969
Posts
7,686

## is there any logic?

Or I&#039;m stupid and can&#039;t find it. Can you explain how the conversion to the alphanummeric &#039;number&#039; should go?

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Aphanumeric Counter...

&#062; It looks easy but it isn&#039;t<BR><BR>No, it&#039;s trivial. <BR><BR>&#062; This is a good exercise for all...<BR><BR>For all who want to help this student do his homework.<BR><BR>**************<BR><BR>Come on now, if you&#039;re going to ask for help with homework, at least ask honestly!<BR><BR>If this really is *NOT* homework, then explain what else it could possibly be for. The solution would take...ummm... Well, four lines if you wanted to write the code neatly. But it could all be done in one (kind of ugly) line if you really wanted to.<BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

Ignore the last 3 digits. They just stay there untouched.<BR><BR>Then the rest of the number--that is, the number of thousands--cycles from 0 to 25 (A to Z) for 26 times (prefix A to Z).<BR><BR>I don&#039;t want to give the four line (or one ugly line) solution here because I think this is a kid trying to get us to do his homework for him. It&#039;s probably question number 17 on his take-home examination.<BR><BR>HEY! I just noticed that is examples are wrong!!!<BR><BR>A=0, B=1, C=2, etc., up to Z=25, right?<BR><BR>So AA999 is 0*26000+0*1000+999 or 0999 okay?<BR><BR>But then AZ999 should be 0*26000+25*1000+999 or 25999 and he has it as 26999!<BR><BR>And then BA000 should be 1*26000+0*1000+000 or 26000 and he has it as 27000.<BR><BR>Tch!<BR><BR>Did the instructor screw up the quiz or did Chrisitian transcribe it wrong? Hmmm...<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
7,686

## Yes, I thought something like that, but

I (yes blame me) *assumed* his examples would be correct. Also I was just as sceptic as you were so I wanted to know *if* he could explain it to me.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Heh! And you know what...

...it isn&#039;t all *that* ugly as a one-line function. But probably only a handful fewer characters of typing than the four-line version.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Good night!

Off to bed. You get to answer Christian if he comes back with some reason other than homework that sounds "real" to you.<BR><BR>See if you can do it a one-line function (well, except for the FUNCTION and END FUNCTION lines, of course), just for the fun of it.<BR><BR>

8. Junior Member
Join Date
Dec 1969
Posts
12

## RE: It's dead easy, Dutch...

I’m working on hotel reservations system.. <BR>The function is to give the reservation code on an alphanueric format to be easier to remember. Instead of using the unique ID that the database assigns to each registration, I want to convert this on alphanumeric code.. <BR>It can be that the tiredness don’t permit me to think correctly, but what looks like a simple function, was converted after working in her during 3 hours in somewhat complicated, and why do i have to re-invent the wheel if perhaps someone in this forum already had to resolve some similar problem.. <BR><BR>It’s been a long time that I’m not a kid... (I’m 26 years old) but It’s very difficult for my explaing you my problem because my natural language is not english, is spanish.... <BR><BR>Thanks for your help...<BR>

9. Junior Member
Join Date
Dec 1969
Posts
12

## Not so easy.......

I’m working on hotel reservations system.. <BR>The function is to give the reservation code on an alphanueric format to be easier to remember. Instead of using the unique ID that the database assigns to each registration, I want to convert this on alphanumeric code.. <BR>It can be that the tiredness don’t permit me to think correctly, but what looks like a simple function, was converted after working in her during 3 hours in somewhat complicated, and why do i have to re-invent the wheel if perhaps someone in this forum already had to resolve some similar problem.. <BR><BR>It’s been a long time that I’m not a kid... (I’m 26 years old) but It’s very difficult for my explaing you my problem because my natural language is not english, is spanish.... <BR><BR>Thanks for your help...<BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## Okay...since it isn't homework...

0032 &#062;&#062; AA032<BR>1000 &#062;&#062; AB000<BR>2123 &#062;&#062; AC123<BR>25999 &#062;&#062; AZ999<BR>26000 &#062;&#062; BA000<BR>26111 &#062;&#062; BA111<BR>51999 &#062;&#062; BZ999<BR>52000 &#062;&#062; CA000<BR>etc.<BR><BR>The principle is simple:<BR>A=0, B=1, C=2, ... Z=25<BR><BR>And the two characters simply represent the first character multiplied by 26 and then added to the second one.<BR><BR>So CD, for example is ( ( C * 26 ) + D ) or ( ( 2 * 26 ) + 3 ) or 55. So 55123 would be CD123.<BR><BR>&#060;%<BR>Sub EncodeResNumber( num )<BR>&nbsp; &nbsp; &#039; to start, capture only the thousands<BR>&nbsp; &nbsp; c1 = num 1000 &#039; the operator does INTEGER division<BR>&nbsp; &nbsp; &#039; How many times does 26 go into c1, then?<BR>&nbsp; &nbsp; c2 = c1 26 <BR>&nbsp; &nbsp; &#039; And then adjust c1 to ignore multiples of 26<BR>&nbsp; &nbsp; c1 = c1 MOD 26<BR>&nbsp; &nbsp; &#039; now convert each of those to a character...<BR>&nbsp; &nbsp; &#039; ... by adding each to the numerical representation of "A"<BR>&nbsp; &nbsp; c1 = ASC("A") + c1<BR>&nbsp; &nbsp; c2 = ASC("A") + c2<BR>&nbsp; &nbsp; &#039; and change the numbers into characters...<BR>&nbsp; &nbsp; c1 = CHR( c1 )<BR>&nbsp; &nbsp; c2 = CHR( c2 )<BR>&nbsp; &nbsp; &#039; now we need the three righthand digits...<BR>&nbsp; &nbsp; &#039; we get them by doing string manipulation:<BR>&nbsp; &nbsp; r3 = CSTR( 1000 + num ) &#039; add 1000 to ensure leading zeroes...<BR>&nbsp; &nbsp; r3 = RIGHT( r3, 3 ) &#039; ... when we chop off last 3 characters<BR>&nbsp; &nbsp; &#039; and our final answer:<BR>&nbsp; &nbsp; EncodeResNumber = c2 & c1 & r3<BR>End Function<BR>%&#062;<BR><BR>That&#039;s the *REALLY* long version of the code. Here&#039;s the version I wrote first:<BR><BR>&#060;%<BR>Sub EncodeResNumber( num )<BR>&nbsp; &nbsp; c1 = num 1000 <BR>&nbsp; &nbsp; c2 = c1 26 <BR>&nbsp; &nbsp; c1 = c1 MOD 26<BR>&nbsp; &nbsp; EncodeResNumber = CHR(65+c2) & CHR(65+c1) & RIGHT( CSTR(1000+num), 3)<BR>End Function<BR>%&#062;<BR><BR>And, finally, here is the "one line" version (it uses 3 lines on the screen, to make it readable, but it&#039;s only one line of VBScript code.<BR><BR>&#060;%<BR>Sub EncodeResNumber( num )<BR>&nbsp; &nbsp; EncodeResNumber = CHR( 65 + (num26000) ) _<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; & CHR( 65 + ( (num1000) MOD 26 ) ) _<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; & RIGHT( CSTR(1000+num), 3)<BR>End Function<BR>%&#062;<BR><BR>Incidentally, your English is quite good! So good that you fooled me into thinking you were a young American student. So English is not the problem.<BR><BR>

#### Posting Permissions

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