Re: URGENT! How to convert 10000 to A000

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I think that we might be able to help you better if you were tell us why the
ID needs to be encoded in this way.

Is it because it must fit in with some immutable legacy system that has a
limitation of 4 characters for an ID of some description?

Is it because someone somewhere thought that would look nice.

Is it because someone wrote some code once that never took into account that
more than 9999 ID's would be required someday?

Is it because some 'boss' somewhere is fklexing his/her muscles?

In addition, it would be helpful if you were to share the actual rules. We
know the rules for when the value is 1 to 9999 and 10000 and 10001. What is
the rule for 0 - is 0 allowed? What is the rule for 10999 - is it A999? What
is the rule for 11000 - is it B000 or even AA00 perhaps? When should a lower
case alpha character be used? What value do you expect ZZZZ to represent?

When and only when you defined the rules then the actual coding should be
relatively easy.


"Niyazi" <Niyazi@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F5D8BD64-1CC9-4382-B60F-BDBCFD35BC84@xxxxxxxxxxxxxxxx
Hi Stephany,

Okay I did what you suggest and it worked upto 9999 but in the ID Number:
10000 shows as 07PS and
10001 shows as 07PT

But the report that I have to construct state that the
number 10000 must be shown as A000 and
number 10001 must be shown A001 and so on.

How to use the algorithm in your code so I can get after 9999 as A000 and
so
on?

Thank you for your kind help.

Rgds,
Niyazi

"Stephany Young" wrote:

OK. I see where you driving at now.

The basis of the algorithm I demonstrated is still valid but will just
need
some amendments to hadle the first 9999 integers.

Insert some code to check for the value of the ID. If it is < 10000 then
just return it to as value.ToString("0000"). If it is >= 10000 then apply
the rest of the algorithm.

When decoding the ID, first try to do an Integer.Parse on the 4 character
string. If it is sucessful then return the result of the Integer.Parse
otherwise continue to decode it according to the algorithm.


"Niyazi" <Niyazi@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2A06E277-00BB-4917-A5E2-261E711FEF9A@xxxxxxxxxxxxxxxx
Hi Stephany,

The encoding was good way to achive the ID that wiil goto around
1,600,000.
But the report that I have to construct has specific requirment.

In your way of encoding shows integer 10 as 000A, but I have to createa
report in below order as well as I have to consider that
The input should be from 1 to say 999,999 as integer. As you can see
the
input lenght might be 1 to 6 but output must be lenght of 4 and in
specific
order as:


0001
0002
...
...
9999
A000
A001
A002
...
...
Aa00
Aa01
Aa02
...
...
Ab00
Ab01
Ab02
...
...
Az00
Az01
Az02
...
...
B000
B001
...
...
...
...
Ba00
Ba01
...
...

As you can see the number 9999 will have to show as 9999 but only 10000
will
jump to as A000 ....A001 ....A002

I guess it is way over me to achive it. I might still searching the
right
answer.
Or might use Randy's suggestion and stuck with ID upto 61999.

I thank you for your kind response and trying to help me out. I wish I
can
get better answer but I am too new to this kind of coding as well as
the
report I have to create is very specific.

Thank you very much.

Rgds,
Niyazi



"Stephany Young" wrote:

If you are talking about alpha-numeric codes comprising any
combination
of
0-9 and A-Z then you can encode values from 0 to 1,679,615 inclusive
(thats
1,679,616 combinations or 36 ^ 4 - 1). If you need more then you can
add
a-z
to your character set and you can then have 62 ^ 4 - 1 or 14,776,336
combinations from 0 to 14,776,335 inclusive.

For 0-9 and A-Z try:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Console.WriteLine(EncodeValue(0))

Console.WriteLine(EncodeValue(1))

Console.WriteLine(EncodeValue(1679615))

Console.WriteLine(DecodeValue("0000"))

Console.WriteLine(DecodeValue("0001"))

Console.WriteLine(DecodeValue("ZZZZ"))

End Sub

Private Function EncodeValue(ByVal value As Integer) As String

If value < 0 OrElse value > (36 ^ 4 - 1) Then Return String.Empty

Try
Dim _chars() As Char = New Char(35) {"0"c, "1"c, "2"c, "3"c,
"4"c,
"5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c,
"G"c,
"H"c, "I"c, "J"c, "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c,
"S"c,
"T"c, "U"c, "V"c, "W"c, "X"c, "Y"c, "Z"c}
Dim _result As String = String.Empty
For _i As Integer = 0 To 3
Dim _work As Integer = value \ CType(36 ^ _i, Integer) Mod 36
_result = _result.Insert(0, _chars(_work))
value -= _work * CType(36 ^ _i, Integer)
Next
Return _result
Catch
Return String.Empty
End Try

End Function

Private Function DecodeValue(ByVal value As String) As Integer

If value Is Nothing OrElse value.Length <> 4 Then Return -1

Try
Dim _chars() As Char = New Char(35) {"0"c, "1"c, "2"c, "3"c,
"4"c,
"5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c,
"G"c,
"H"c, "I"c, "J"c, "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c,
"S"c,
"T"c, "U"c, "V"c, "W"c, "X"c, "Y"c, "Z"c}
Dim _result As Integer = 0
For _i As Integer = 0 To 3
_result += Array.IndexOf(_chars, value.Chars(_i)) * CType(36 ^
(3 -
_i), Integer)
Next
Return _result
Catch
Return -1
End Try

End Function

For 0-9, A-Z and a-z, extend the character arrays and the loop limits
accordingly.


"Niyazi" <Niyazi@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:70528117-7510-4C7B-AB2A-1A4C0EF562FD@xxxxxxxxxxxxxxxx
Hi Randy,

I am sure ID number will not reach the 1,000,000 point. But if I
have a
integer number between 1 to 999,999 then is it possible to convert
starting
from
0001 (zero, zero, zero one) up to zzzz. After riches ZZZZ than have
to
start
something like aaaa. Or I am thinking like below example

Example:
0001
0002
...
...
9999
A000
A001
A002
...
...
Aa00
Aa01
Aa02
...
...
Ab00
Ab01
Ab02
...
...
Az00
Az01
Az02
...
...
B000
B001
...
...
...
...
Ba00
Ba01
...
...
Bb..

I am gussing this way may be I am not riching the 999,999 but at
least
I
will get
Id no around 300,000 and that will be okay with me.

I try to implement in short time above example to your code, but I
find
I
am
not qualified that extend to find the answer.

I am also thinking to use some kind encoding method that converts
six
digit
Integer number into alphanumeric data that has only lentgh of 4.

But again I am not sure where to start. I search the net and I
couldn't
find
any good article or example.

One article about "Random Alphanumeric String Generation" uses the
randomize
methot to genereate a password. But I my answer wasn't in that.

Say If see a alphanumeric charecter in the report such as zza5 then
I
will
have to convert into 6 digit integer. But my main goal is know to
take
the
6
digit integer and convert to alphanumeric characters that has lenthf
of
4
and
vice-versa.

I hope you can help me one more time.

I thank you for your kind response.

Rgds,
Niyazi











.


Quantcast