用bitarray类和BitVector32类实现
创新互联建站专注于企业营销型网站、网站重做改版、五常网站定制设计、自适应品牌网站建设、HTML5、购物商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为五常等各大城市提供网站开发制作服务。
VB实现大致如下:
Imports System.Collections.Specialized
...
Dim bvData as BitVector32
Dim sec1 As BitVector32.Section = BitVector32.CreateSection(255)
Dim sec2 As BitVector32.Section = BitVector32.CreateSection(255,sec1)
Dim sec3 As BitVector32.Section = BitVector32.CreateSection(255,sec2)
Dim sec4 As BitVector32.Section = BitVector32.CreateSection(255,sec3)
Dim Buf(4096) as Byte 'receive byte size matched with DSP send data: 1024*4Byte
Dim recCount as Integer
Dim Data(1024) as long
....
For recCount = 0 To 1023
bvData(sec4) = buf(recByte*4)
bvData(sec3) = buf(recByte*4+1)
bvData(sec2) = buf(recByte*4+2)
bvData(sec1) = buf(recByte*4+3)
Data(recCount) = bvData.Data '这样数据肯定是-398
Next
....
代码分析:
Math.Floor:返回小于或等于指定数字的最大整数。
Fix:返回数字的整数部分。
Math.Floor 和 Fix 函数都移除 Number 的小数部分并返回得到的整数值。
Math.Floor 和 Fix 函数之间的区别在于:如果 Number 为负,则 Math.Floor 返回小于或等于 Number 的第一个负整数,而 Fix 返回大于或等于 Number 的第一个负整数。例如,Math.Floor 将 -8.4 转换成 -9,而 Fix 将 -8.4 转换成 -8。
你的要求:如果你的要求是直接舍去,那么应当采用Fix方法,考虑到时负数的情况,那么采用Math.Floor将会出现非预期的结果。
测试结果:见附图。
欢迎交流。
'自定义一个函数:功能是给小数直接进一位
'num 要操作的小数
'n 保留几位小数,
Function getNumber(num As Double, n As Integer) As Double
Dim a As Double
a = Int(num * (10 ^ n)) / (10 ^ n)
If a = num Then
getNumber = a
Else
getNumber = a + 1 / (10 ^ n)
End If
End Function
-------------------------
调用一下:
Private Sub Command1_Click()
MsgBox (getNumber(3.1415926, 2))
End Sub
得到3.15
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