• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

Decimal to Alphanumeric

I am writing a VB.Net to convert a decimal to customized alphanumeric pattern.

I know this is sounds like Dec 2 Hex... but I really can't make it from my mind.

here the Alphanumeric pattern:
"0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y"

from the list there are no "I,O,U,Z" because they are too similar to 0,1,2 and V, that's why they have been taken out.

I want to create a function to convert a specific  numeric to alphanumeric .

Can anyone help?
0
cwteoh
Asked:
cwteoh
  • 7
  • 3
  • 3
  • +3
2 Solutions
 
JackOfPHCommented:
Can you give more example?
0
 
infochandruCommented:
I hope this will help you

private void button1_Click(object sender, EventArgs e)
        {
            string[] strArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y" };
            string toFormat = "";
            int val = Convert.ToInt32(textBox1.Text);
            while (val > 0)
            {
                int rem = val % 32;
                toFormat += strArray[rem];
                val = val / 32;

            }
            textBox2.Text = toFormat;
        }
0
 
cwteohAuthor Commented:
OK...
If I want to convert 10 in decimal to Alphanumeric it should be "A"
or if 20 in decimal = "L"
What if 1000?
What if 250?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
appariCommented:
you are basically want to develop base 32 number system.
in your number system total 32 digits are there 0-9 and A-Y (excluding I,O,U) A-10, B-11,...Y-31
to convert decimal number to new number system
divide your decimal number with 32 and write down the reminder as follows and continue till you get 0 as division result.

for an example lets convert 156 to new base 32 number
divide 156 with 32  156/32 = 4 reminder 28(=V in new number system)
now divide result from previous step 4 with 32 4/32 =0 reminder 4(=4in new number system)
now we got the result as 0 in previous step so stop here. now write all the reminders from bottom to top beside each other in our sample its 4V
so 156 in decimal is equal to 4V in new system.

hope you got the logic, now build the code.
0
 
cwteohAuthor Commented:
OK~~~ if I can understand what you are saying... I should try ...
0
 
infochandruCommented:
Let me know what is your expected result
if 1000 then ?
0
 
cwteohAuthor Commented:
This is the problem I got...
I do not have the right answer to compare.
I just come out a short code like this (VB.Net)
 

Function Dec2AlphaNumeric(ByVal intDec As String) As String
Const charSeqStr = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y"
Dim toFormat As String = "" 
Dim Pattern() As String = charSeqStr.Split(",")
Dim val As Double = Convert.ToInt32(intDec)
Dim i As Integer = 0
Do Until val <= 0
i = val Mod 32
toFormat = (Pattern(i) & toFormat)
val = System.Math.Floor(val / 32)
Loop
Return toFormat


End Function
0
 
cwteohAuthor Commented:
My 1000 answer is Y8... is that right?.
0
 
infochandruCommented:
Hope this got '1000'  to 'Y8'

        Dim strArray() As String
        strArray = New String() {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y"}
        Dim toFormat As String
        Dim val As Integer
        toFormat = ""
        val = Convert.ToInt32(TextBox1.Text)
        While (val > 0)
            Dim remain As Integer
            remain = val Mod 32
            toFormat = strArray(remain) + toFormat
            val = System.Math.Floor(val / 32)
        End While
        TextBox2.Text = toFormat
0
 
appariCommented:
yes 1000 is Y8
to check if the result is correct or not convert base 32 number to decimal again using the following logic

Y8 = Y*(32^1) + 8 * (32 ^ 0)
^ is power function (example 32^1 = 32, 2^3 = 2*2*2 = 8)
Y=31
Y8 = 31*(32^1) + 8 * (32 ^ 0) = 31*32 + 8*1 = 992 + 8 = 1000
0
 
Jeff CertainCommented:
I'm curious. What's the motivation for this conversion?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
See my previous answer here: http://www.experts-exchange.com/Programming/Misc/Q_22001532.html#a17593224

Hit the Button repeatedly and watch closely at the sequences it produces...

Does it "count" the way you want it to?
Public Class Form1

    Private Rev As New Revision("0123456789ABCDEFGHJKLMNPQRSTVWXY", "0")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = Rev.CurrentRevision
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = Rev.NextRevision
    End Sub

End Class

Public Class Revision

    Private chars As String
    Private values() As Char
    Private curRevision As System.Text.StringBuilder

    Public Sub New()
        Me.DefaultRevision()
    End Sub

    Public Sub New(ByVal validChars As String)
        If validChars.Length > 0 Then
            chars = validChars.ToUpper()
            values = chars.ToCharArray()
            curRevision = New System.Text.StringBuilder(values(0))
        Else
            Me.DefaultRevision()
        End If
    End Sub

    Public Sub New(ByVal validChars As String, ByVal startingRevision As String)
        Me.New(validChars)
        curRevision = New System.Text.StringBuilder(startingRevision.ToUpper())
        Dim i As Integer
        For i = 0 To curRevision.Length - 1
            If Array.IndexOf(values, curRevision.Chars(i)) = -1 Then
                curRevision = New System.Text.StringBuilder(values(0))
                MessageBox.Show("Revision has been reset." & vbCrLf & "Current Revision = " & Me.CurrentRevision, "Starting Revision contains an Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit For
            End If
        Next
    End Sub

    Private Sub DefaultRevision()
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        values = chars.ToCharArray
        curRevision = New System.Text.StringBuilder(values(0))
    End Sub

    Public ReadOnly Property ValidChars() As String
        Get
            Return chars
        End Get
    End Property

    Public ReadOnly Property CurrentRevision() As String
        Get
            Return curRevision.ToString()
        End Get
    End Property

    Public Function NextRevision(Optional ByVal numRevisions As Integer = 1) As String
        Dim forward As Boolean = (numRevisions > 0)
        numRevisions = Math.Abs(numRevisions)
        Dim i As Integer
        For i = 1 To numRevisions
            If forward Then
                Me.Increment()
            Else
                Me.Decrement()
            End If
        Next
        Return Me.CurrentRevision
    End Function

    Private Sub Increment()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index < (chars.Length - 1) Then
            index = index + 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(0)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index < (values.Length - 1) Then
                    index = index + 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(0)
                End If
            Next
            curRevision.Insert(0, values(0))
        End If
    End Sub

    Private Sub Decrement()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index > 0 Then
            index = index - 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(values.Length - 1)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index > 0 Then
                    index = index - 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(values.Length - 1)
                End If
            Next
            curRevision.Remove(0, 1)
            If curRevision.Length = 0 Then
                curRevision.Insert(0, values(0))
            End If
        End If
    End Sub

End Class

Open in new window

0
 
cwteohAuthor Commented:
Chaosian,
Someone is trying to be special than other.... they don't want to use Hex, Binary, or Octa.... but for really reason... I totally no clue. I never ask as well.
0
 
cwteohAuthor Commented:
Alright!!! Thanks for your contribution... I manage to solve my problem ...
Appari> you have a really good explaination...
Infochandru> Thanks for your formula. That really simple than I thought.

Idle_Mind> I am going to look through your code...

Thanks again....
0
 
Jeff CertainCommented:
*chuckles*
I just thought it might be worth pointing out that, if the goal is to compress the data, this approach will actually take more space than storing numbers as numbers
0
 
cwteohAuthor Commented:
really? I never know that... because if ascii?
0
 
Jeff CertainCommented:
Well, the issues amounts to this. Integers are either 32-bit (4 bytes) or 64-bit (8 bytes) depending on the hardware architecture.
Strings are 1 byte (ASCII) or 2 bytes (unicode) per character.
So, if you have a large number (say 32x32x32) your approach will require 4 bytes or 8 bytes, depending on the character set. On the other hand, this is only a 16-bit integer (2 bytes). Now, admittedly, you'll likely use the Integer type to store it, so it's going to take 4 or 8 bytes depending on the architecture. However, this is the break-even point. After this, the advantage is with the numeric type.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now