例如:将0.09997866 转化成 3D CC C1 9D
请大家帮忙,谢谢
16 个解决方案
#1
嗯 你如果用手工是如何转的呢?
#2
把这个数乘以10000000,然后再转就行了;
byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
直接这么传就行了。
byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
直接这么传就行了。
#3
Dim F As Single = 0.5
Dim B() As Byte = System.BitConverter.GetBytes(F)
#4
以前在没有这个方法的时候,我就把浮点数以Binary方式写入文件,在用字节数组读出来的,很土。
#5
你可以不看CSDN...不能不看MSDN...
Dim b As Byte() = BitConverter.GetBytes(i)
Dim l As List(Of Byte) = New List(Of Byte)(b)
l.Reverse()
Return l.ToArray()
#6
你的结果是:57 57 57 55 56 54 56 54,不是3D CC C1 9D
应该怎么写代码呢?
我不是很懂,谢谢
#7
#8
Dim b As Byte() = BitConverter.GetBytes(f)
Array.Reverse(b)
Console.Write(BitConverter.ToString(b).Replace("-"," "))
#9
Public Function funSignSngTo4Byt(SingleData As Single) As Boolean
'高位在前,低位在后
Dim i, d As Integer
Dim PorN As Byte
Dim ExpVal, temp As Long
Dim FraVal, t1 As Long
Dim SingleFra, tempval As Single
If SingleData = 0 Then
For i = 0 To 3
gbytBinData(i) = 0
Next i
Exit Function
End If
If SingleData >= 0 Then '符号位
PorN = 0
Else
PorN = &H80
SingleData = -1 * SingleData '变为正数
End If
ExpVal = Int(Log(SingleData) / Log(2)) '指数
If ExpVal > 128 Then
funSignSngTo4Byt = False
Exit Function
End If
If ExpVal = 127 Then
SingleFra = SingleData / (2 ^ ExpVal)
Else
SingleFra = SingleData / (2 ^ ExpVal) - 1
End If
FraVal = Int(SingleFra * 128 * 256 * 256) And &HFFFFFF
ExpVal = ExpVal + 127
gbytBinData(0) = PorN + ExpVal \ 2
gbytBinData(3) = FraVal And &HFF
FraVal = Int(FraVal \ 256)
gbytBinData(2) = FraVal Mod 256
FraVal = FraVal \ 256
gbytBinData(1) = (FraVal Mod 256) Or ((ExpVal And 1) * 128)
End Function
'高位在前,低位在后
Dim i, d As Integer
Dim PorN As Byte
Dim ExpVal, temp As Long
Dim FraVal, t1 As Long
Dim SingleFra, tempval As Single
If SingleData = 0 Then
For i = 0 To 3
gbytBinData(i) = 0
Next i
Exit Function
End If
If SingleData >= 0 Then '符号位
PorN = 0
Else
PorN = &H80
SingleData = -1 * SingleData '变为正数
End If
ExpVal = Int(Log(SingleData) / Log(2)) '指数
If ExpVal > 128 Then
funSignSngTo4Byt = False
Exit Function
End If
If ExpVal = 127 Then
SingleFra = SingleData / (2 ^ ExpVal)
Else
SingleFra = SingleData / (2 ^ ExpVal) - 1
End If
FraVal = Int(SingleFra * 128 * 256 * 256) And &HFFFFFF
ExpVal = ExpVal + 127
gbytBinData(0) = PorN + ExpVal \ 2
gbytBinData(3) = FraVal And &HFF
FraVal = Int(FraVal \ 256)
gbytBinData(2) = FraVal Mod 256
FraVal = FraVal \ 256
gbytBinData(1) = (FraVal Mod 256) Or ((ExpVal And 1) * 128)
End Function
#10
真奇怪啊,我不是早给了答案了吗?怎么在争论?
如果要倒序数组,请使用Array.Reverse 方法
如果要倒序数组,请使用Array.Reverse 方法
Dim f As Single = 0.09997866
Dim B() As Byte = System.BitConverter.GetBytes(f)
Array.Reverse(B)
#11
真奇怪啊,我不是早给了答案了吗?怎么又重复一遍?
#12
你可以不看CSDN...不能不看MSDN...
#13
JF
#14
路过,学习一下!
#15
最简单高效的方法……
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionSingle
<FieldOffset(0)> Public Single0 As Single
<FieldOffset(0)> Public Int0 As Integer
<FieldOffset(0)> Public Byte0 As Byte
<FieldOffset(1)> Public Byte1 As Byte
<FieldOffset(2)> Public Byte2 As Byte
<FieldOffset(3)> Public Byte3 As Byte
Public Function GetHexString() As String
Return Hex(Int0)
End Function
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionSingle
<FieldOffset(0)> Public Single0 As Single
<FieldOffset(0)> Public Int0 As Integer
<FieldOffset(0)> Public Byte0 As Byte
<FieldOffset(1)> Public Byte1 As Byte
<FieldOffset(2)> Public Byte2 As Byte
<FieldOffset(3)> Public Byte3 As Byte
Public Function GetHexString() As String
Return Hex(Int0)
End Function
End Structure
#16
这个很好
#1
嗯 你如果用手工是如何转的呢?
#2
把这个数乘以10000000,然后再转就行了;
byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
直接这么传就行了。
byte[] bytes = System.Text.Encoding.Default.GetBytes(str);
直接这么传就行了。
#3
Dim F As Single = 0.5
Dim B() As Byte = System.BitConverter.GetBytes(F)
#4
以前在没有这个方法的时候,我就把浮点数以Binary方式写入文件,在用字节数组读出来的,很土。
#5
你可以不看CSDN...不能不看MSDN...
Dim b As Byte() = BitConverter.GetBytes(i)
Dim l As List(Of Byte) = New List(Of Byte)(b)
l.Reverse()
Return l.ToArray()
#6
你的结果是:57 57 57 55 56 54 56 54,不是3D CC C1 9D
应该怎么写代码呢?
我不是很懂,谢谢
#7
#8
Dim b As Byte() = BitConverter.GetBytes(f)
Array.Reverse(b)
Console.Write(BitConverter.ToString(b).Replace("-"," "))
#9
Public Function funSignSngTo4Byt(SingleData As Single) As Boolean
'高位在前,低位在后
Dim i, d As Integer
Dim PorN As Byte
Dim ExpVal, temp As Long
Dim FraVal, t1 As Long
Dim SingleFra, tempval As Single
If SingleData = 0 Then
For i = 0 To 3
gbytBinData(i) = 0
Next i
Exit Function
End If
If SingleData >= 0 Then '符号位
PorN = 0
Else
PorN = &H80
SingleData = -1 * SingleData '变为正数
End If
ExpVal = Int(Log(SingleData) / Log(2)) '指数
If ExpVal > 128 Then
funSignSngTo4Byt = False
Exit Function
End If
If ExpVal = 127 Then
SingleFra = SingleData / (2 ^ ExpVal)
Else
SingleFra = SingleData / (2 ^ ExpVal) - 1
End If
FraVal = Int(SingleFra * 128 * 256 * 256) And &HFFFFFF
ExpVal = ExpVal + 127
gbytBinData(0) = PorN + ExpVal \ 2
gbytBinData(3) = FraVal And &HFF
FraVal = Int(FraVal \ 256)
gbytBinData(2) = FraVal Mod 256
FraVal = FraVal \ 256
gbytBinData(1) = (FraVal Mod 256) Or ((ExpVal And 1) * 128)
End Function
'高位在前,低位在后
Dim i, d As Integer
Dim PorN As Byte
Dim ExpVal, temp As Long
Dim FraVal, t1 As Long
Dim SingleFra, tempval As Single
If SingleData = 0 Then
For i = 0 To 3
gbytBinData(i) = 0
Next i
Exit Function
End If
If SingleData >= 0 Then '符号位
PorN = 0
Else
PorN = &H80
SingleData = -1 * SingleData '变为正数
End If
ExpVal = Int(Log(SingleData) / Log(2)) '指数
If ExpVal > 128 Then
funSignSngTo4Byt = False
Exit Function
End If
If ExpVal = 127 Then
SingleFra = SingleData / (2 ^ ExpVal)
Else
SingleFra = SingleData / (2 ^ ExpVal) - 1
End If
FraVal = Int(SingleFra * 128 * 256 * 256) And &HFFFFFF
ExpVal = ExpVal + 127
gbytBinData(0) = PorN + ExpVal \ 2
gbytBinData(3) = FraVal And &HFF
FraVal = Int(FraVal \ 256)
gbytBinData(2) = FraVal Mod 256
FraVal = FraVal \ 256
gbytBinData(1) = (FraVal Mod 256) Or ((ExpVal And 1) * 128)
End Function
#10
真奇怪啊,我不是早给了答案了吗?怎么在争论?
如果要倒序数组,请使用Array.Reverse 方法
如果要倒序数组,请使用Array.Reverse 方法
Dim f As Single = 0.09997866
Dim B() As Byte = System.BitConverter.GetBytes(f)
Array.Reverse(B)
#11
真奇怪啊,我不是早给了答案了吗?怎么又重复一遍?
#12
你可以不看CSDN...不能不看MSDN...
#13
JF
#14
路过,学习一下!
#15
最简单高效的方法……
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionSingle
<FieldOffset(0)> Public Single0 As Single
<FieldOffset(0)> Public Int0 As Integer
<FieldOffset(0)> Public Byte0 As Byte
<FieldOffset(1)> Public Byte1 As Byte
<FieldOffset(2)> Public Byte2 As Byte
<FieldOffset(3)> Public Byte3 As Byte
Public Function GetHexString() As String
Return Hex(Int0)
End Function
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionSingle
<FieldOffset(0)> Public Single0 As Single
<FieldOffset(0)> Public Int0 As Integer
<FieldOffset(0)> Public Byte0 As Byte
<FieldOffset(1)> Public Byte1 As Byte
<FieldOffset(2)> Public Byte2 As Byte
<FieldOffset(3)> Public Byte3 As Byte
Public Function GetHexString() As String
Return Hex(Int0)
End Function
End Structure
#16
这个很好