积极答复者
32位浮点数用BitConverter.GetBytes和StringBuilder得到十六进制数并格式化成8个十六进制字条

问题
-
Dim f As Single = 25.1F
Dim b As Byte() = BitConverter.GetBytes(f)
Dim sb As New StringBuilder()
For Each by As Byte In b
sb.Append(by.ToString("X"))
Next
这样sb="CDCCC841" 为有效8个字符的32位十六进制浮点数。但当f=100时得到“0042C8”,于是我认为第一个字符应该是“00”,第二个字符也应该是“00”,这样得到的结果是我想要的“000042C8”。请教如何格式化才能得到这样的结果呢?
答案
-
你好,
可以把循环反一下。像这样:
Dim f As Single = 25.1F Dim b As Byte() = BitConverter.GetBytes(f) Dim sb As New StringBuilder() For i As Integer = b.Length - 1 To 0 Step -1
sb.Append(b(i).ToString("X"))
Next
Best regards,
Cole Wu
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已编辑 Zhanglong WuMicrosoft contingent staff, Moderator 2016年8月30日 4:39
- 已标记为答案 月满西楼 2016年8月30日 9:30
- 取消答案标记 月满西楼 2016年8月30日 9:31
- 已标记为答案 月满西楼 2016年8月30日 9:34
全部回复
-
你好,
可以把循环反一下。像这样:
Dim f As Single = 25.1F Dim b As Byte() = BitConverter.GetBytes(f) Dim sb As New StringBuilder() For i As Integer = b.Length - 1 To 0 Step -1
sb.Append(b(i).ToString("X"))
Next
Best regards,
Cole Wu
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已编辑 Zhanglong WuMicrosoft contingent staff, Moderator 2016年8月30日 4:39
- 已标记为答案 月满西楼 2016年8月30日 9:30
- 取消答案标记 月满西楼 2016年8月30日 9:31
- 已标记为答案 月满西楼 2016年8月30日 9:34