vb.net将数字转换为等效的字母数字
我需要将四位数字转换为三位字母数字,其中 001-999=001-999vb.net将数字转换为等效的字母数字,vb.net,Vb.net,我需要将四位数字转换为三位字母数字,其中 001-999=001-999 1000=00A 1027=01A 等等。。最多=ZZZ 我对编程非常陌生,不知道如何处理这个问题。任何帮助都将不胜感激。这对您有何帮助 Private Function ConvertMyNumber(toConvert As UShort) As String Const MaxValueToConvert = 3885S If (toConvert > MaxValueToConvert) Th
1000=00A
1027=01A 等等。。最多=ZZZ
我对编程非常陌生,不知道如何处理这个问题。任何帮助都将不胜感激。这对您有何帮助
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
私有函数ConvertMyNumber(toConvert作为UShort)作为字符串
常量MaxValueToConvert=3885S
如果(toConvert>MaxValueToConvert),则抛出新的ArgumentException(String.Format(“参数值{0}超过最大可能值{1}.”,toConvert,MaxValueToConvert))
常量NumeringSchemeThreshold=1000
常数TotalResultLength=3
常数PaddingChar=“0”c
如果是(toConvert=firstStepThreshold,numCycles=secondStepThreshold)然后
numCycles=numCycles-第二步阈值
后缀=LastAlphabetChar和LastAlphabetChar
prefix=String.Empty
如果结束
常量AsciCharValueOffset=65
'将循环编号转换为转换后的修改字母,并返回零填充结果。
返回(前缀和CChar(Char.ConvertFromUtf32(moddedValue+asciharvalueoffset))和后缀)。paddleft(TotalResultLength,PaddingChar)
端函数
即使您是编程新手,您也应该知道您使用的是哪种数字。谁说你应该改变他们的信仰?那个人应该告诉你怎么做。@Mike--如果1000是00A,那么1026是01A,1027是01B,对吗?@TimSchmelter--我认为问题很清楚。@Mike--如果结果大于ZZZ
,会发生什么?它会引发异常吗?现在我接受了你的答案,这段代码运行得很好,有任何价值吗?我需要重新发布问题以获得3599的帮助吗?@Mike--等一下,我会看一看。@Mike,我很清楚3600应该会导致0AZ
,对吗?@Mike--好的,我已经修改了代码。希望它现在对你有用。