cwteoh
asked on
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?
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
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?
Can you give more example?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK~~~ if I can understand what you are saying... I should try ...
Let me know what is your expected result
if 1000 then ?
if 1000 then ?
ASKER
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
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
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
ASKER
My 1000 answer is Y8... is that right?.
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.T ext)
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
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.T
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
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
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
I'm curious. What's the motivation for this conversion?
See my previous answer here: https://www.experts-exchange.com/questions/22001532/using-text-as-counter.html?anchorAnswerId=17593224#a17593224
Hit the Button repeatedly and watch closely at the sequences it produces...
Does it "count" the way you want it to?
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
ASKER
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.
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.
ASKER
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....
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....
*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
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
ASKER
really? I never know that... because if ascii?
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.
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.