此代码用于将Ascii转换为十进制,但我无法在(2位数)左侧添加零
Dim sText As String
Dim i As Integer
Dim lenText As Integer
sText = TextBox6.Text
lenText = Len(sText)
For i = 1 To lenText
sASC = sASC & CStr(Asc(Mid$(sText, i, 1)))
Next i
m = sASC `
答案 0 :(得分:0)
如果我理解正确,你想用结果字符串的两位数的每个十进制值填一个零,那么你可以这样做:
<HideModuleName>
Public Module StringExtensions
<Extension>
Public Function ToDecimals(ByVal sender As String) As String
Dim sb As New StringBuilder(capacity:=(sender.Length * 3))
For Each c As Char In sender
sb.AppendFormat("{0:000}", Convert.ToInt32(c))
Next c
Return sb.ToString
End Function
End Module
用法:
Dim dec As String = "ABC".ToDecimals()
Console.WriteLine(dec)
这会产生:
065066067
Private Sub ButtonCalc_Click() Handles ButtonCalc.Click
Dim input As String = Me.TextBox1.Text
Me.TextBox2.Text = StringToDecimals(input)
End sub
' Rehusable function.
Public Shared Function StringToDecimals(ByVal str As String) As String
Dim sb As New StringBuilder(capacity:=(str.Length * 3))
For Each c As Char In str
sb.AppendFormat("{0:000}", Convert.ToInt32(c))
Next c
Return sb.ToString
End Function
答案 1 :(得分:0)
用一些linq替换注释的代码
'For i = 1 To lenText
' sASC = sASC & CStr(Asc(Mid$(sText, i, 1)))
'Next i
sASC = sText.
Select(Function(t) String.Format("{0:0000}", Asc(t))).
Aggregate(Function(t1, t2) t1 & "," & t2)
它会重新选择你的数字,格式化为总是[至少] 4个字符,任何前导的额外空格为0.然后它将结果放在一起(聚合),加上“,”。
输入:qussai
输出:0113,0117,0115,0115,0097,0105
根据评论中的其他信息判断,字符串不是代码中此时的最佳工具。您有来自Asc()
的数值,然后您将进行数字运算以提高到5次幂。最好使用Asc值来进行数学运算,然后创建一个字符串。这样做是为了获得理想的结果:
sASC = sText.
Select(Function(t) Math.Pow(Asc(t), 5).ToString()).
Aggregate(Function(t1, t2) t1 & t2)
输入:qussai
输出:18424351793219244803572011357187520113571875858734025712762815625