vb.net将数字转换为字母数字等价物

时间:2014-12-10 15:25:46

标签: vb.net

我需要将四位数转换为三位数的字母数字,其中

001-999 = 001-999
1000 = 00A
1027 = 01A

等......无论是什么= ZZZ

我对编程很陌生,无法弄清楚如何处理这个问题。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

这对你有什么用?

Private Function ConvertMyNumber(toConvert As UShort) As String
    Const MaxValueToConvert = 3885S
    If (toConvert > MaxValueToConvert) Then Throw New ArgumentException(String.Format("Argument value of {0} exceeds maximum possible value of {1}.", toConvert, MaxValueToConvert))
    Const NumeringSchemeThreshold = 1000
    Const TotalResultLength = 3
    Const PaddingChar = "0"c
    If (toConvert < NumeringSchemeThreshold) Then Return toConvert.ToString().PadLeft(TotalResultLength, PaddingChar)
    Dim adjustedValue = (toConvert - NumeringSchemeThreshold) 'since we're starting with this numbering scheme at NumeringSchemeThreshold
    Const NumbersInAlphabet = 26
    Dim moddedValue = (adjustedValue Mod NumbersInAlphabet) '0 to NumbersInAlphabet - 1 based on the cycle
    Dim numCycles = Fix(adjustedValue / NumbersInAlphabet) 'What "cycle number" it is, i.e. the number of times around the alphabet loop.
    Dim suffix As String = String.Empty
    Dim prefix As String = CStr(numCycles)
    Const firstStepThreshold = 100
    Const secondStepThreshold = 110
    Const LastAlphabetChar = "Z"c

    If (numCycles >= firstStepThreshold And numCycles < secondStepThreshold) Then
        numCycles = numCycles - firstStepThreshold
        prefix = CStr(numCycles)
        suffix = LastAlphabetChar
    ElseIf (numCycles >= secondStepThreshold) Then
        numCycles = numCycles - secondStepThreshold
        suffix = LastAlphabetChar & LastAlphabetChar
        prefix = String.Empty
    End If

    Const AsciiCharValueOffset = 65

    'concat the cycle number to the converted modded letter, and return the zero-padded result.
    Return (prefix & CChar(Char.ConvertFromUtf32(moddedValue + AsciiCharValueOffset)) & suffix).PadLeft(TotalResultLength, PaddingChar)
End Function