重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
VB.Net自带函数:
在榕城等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、网站设计 网站设计制作定制设计,公司网站建设,企业网站建设,高端网站设计,营销型网站,外贸网站建设,榕城网站建设费用合理。
十进制转八进制:Oct(num)
十六进制转八进制:oct("H" num)
十进制转十六进制:hex(num)
八进制转十六进制:hex("O" num)
十六进制转换为十进制:CLng("H" num)
'写入
Dim bytes() As Byte = {34, 23, 43, 43, 55, 3}
Dim items = (From item In bytes Select item.ToString("000")).ToArray()
System.IO.File.WriteAllLines("c:\test.txt", items)
'读取
Dim items2 = System.IO.File.ReadAllLines("c:\test.txt")
Dim bytes2 = (From item In items2 Select Byte.Parse(item)).ToArray()
For Each item In bytes2
Console.WriteLine(item.ToString())
Next
Not一个数值实际上是对这个数值进行“非”运算,即转换为2进制,0变为1,1变为0,你可以这样试:
For i = 1 To 10
s = i
debug.print not (i Mod 5 )
Next
这样可以看到not (i Mod 5 )这个表达式究竟等于多少。
按照你的想法,应该这样做结果才是对的:
If Not (i Mod 5 0) Then
VB.NET控制台程序:
Module module3
Sub Main()
Dim i As Integer
Dim num As Integer = -35
Dim R As Integer = 8
Dim arr(33) As Integer
Dim len As Integer
arr = TransToR(num, R)
len = arr(0)
Console.Write(num " 转换成 " R " 进制后:" vbTab)
If (arr(1) = 1) Then
Console.Write("-")
End If
For i = len + 1 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToSourceCode(num)
Console.Write(num " 转换成32位二进制源码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToReverseCode(num)
Console.Write(num " 转换成32位二进制反码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
arr = TransToCompensatoryCode(num)
Console.Write(num " 转换成32位二进制补码后:" vbTab)
Console.Write(arr(1))
For i = 32 To 2 Step -1
Console.Write(arr(i))
Next
Console.WriteLine()
End Sub
'将十进制数num转换为R(2=R=9)进制数,结果存在arr数组中
'arr(0)存储长度, arr(1)存储符号位:1为负,0为正
Function TransToR(ByVal num As Integer, ByVal R As Integer) As Integer()
Dim arr(33) As Integer
arr(0) = 0 '初始长度
'设置符号位
If (num 0) Then
arr(1) = 1
Else
arr(1) = 0
End If
num = Math.Abs(num)
While num 0
arr(0) = arr(0) + 1
arr(arr(0) + 1) = num Mod R
num \= R
End While
Return arr
End Function
'将十进制数num转换为二进制原码
Function TransToSourceCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToR(num, 2)
'高位补零
For i = arr(0) + 2 To 32
arr(i) = 0
Next
arr(0) = 32
Return arr
End Function
'将十进制数num转换为二进制反码
Function TransToReverseCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
arr = TransToSourceCode(num)
If num = 0 Then
Return arr
End If
'除符号位外各位取反
For i = 2 To 33
arr(i) = 1 - arr(i)
Next
Return arr
End Function
'将十进制数num转换为二进制补码
Function TransToCompensatoryCode(ByVal num As Integer) As Integer()
Dim arr(33) As Integer
Dim i As Integer
Dim c As Integer = 0 '进位
arr = TransToReverseCode(num)
If num = 0 Then
Return arr
End If
'末尾加1
i = 2
arr(i) += 1
Do While True
arr(i) = c + arr(i)
c = arr(i) \ 2
arr(i) = arr(i) Mod 2
i += 1
If c 1 Then
Exit Do
End If
Loop
Return arr
End Function
End Module