从 vb.net 中的十六进制中提取制造日期
Extracting Manufacture date from Hex in vb.net
我正在使用 SD 卡,当我需要制造日期代码时,成功从 ic 读取 SID 到 vb.net.Problem。数据表说:
The manufacturing date is composed of two hexadecimal digits, one is 8
bits representing the year(y) and the other is 4 bits representing the
month (m). The “m” field [11:8] is the month code. 1 = January. The
“y” field [19:12] is the year code. 0 = 2000. As an example, the
binary value of the Date field for production date “April 2001” will
be: 00000001 0100.
理解这并不难,难的部分是创建一个代码,根据十六进制数字放置日期时间。例如我收到 0x00 0xAC [000000000000000010101100] 我们只需要 12 位(前 4 位保留),这意味着 [000010101100],前 8 位是年份 0x0A = 10(DEC)= 2010,最后 4 位是 mounth [1100 ] = 12(DEC),所以这意味着我们有 12.2010 制造日期。如何用位进行这种操作,然后将结果放入 DateTime?我通过 serialport.readexisting 收到字符串表示形式的 HEX。
我们可以使用 BitArray()
将 2 个十六进制值转换为字节数组中的位表示形式。
然后,Math.Pow()
使用位位置将值转换为十进制表示形式。
new Date()
构造函数用于从 Year 和 Month 的值生成 DateTime 对象,然后转换为所需格式的字符串。
给定两个十六进制值 0x00
0xAC
:
Dim sHexValue1 As String = "0x00"
Dim sHexValue2 As String = "0xAC"
注意第一个字节设置为0xAC
的值,第二个设置为0x00
.
'Convert the two "Hex" strings to byte
Dim bytes As Byte() = {
Convert.ToByte(sHexValue2.Substring(2, 2), 16),
Convert.ToByte(sHexValue1.Substring(2, 2), 16)
}
'Convert the byte array in array of bits. => True means that the bit is on (1)
Dim bArray As New BitArray(bytes)
Dim month As Integer = 0
Dim year As Integer = 0
'Convert to decimal the value expressed by the first 4 bits
For x As Integer = 0 To 3
month += If(bArray(x) = True, CType(Math.Pow(2, x), Integer), 0)
Next
'Convert to decimal the value expressed by bits 4-12
For x As Integer = 0 To 7
year += If(bArray(x + 4) = True, CType(Math.Pow(2, x), Integer), 0)
Next
Dim resultDate = New Date(year + 2000, month, 1).ToString("MM.yyyy")
我正在使用 SD 卡,当我需要制造日期代码时,成功从 ic 读取 SID 到 vb.net.Problem。数据表说:
The manufacturing date is composed of two hexadecimal digits, one is 8 bits representing the year(y) and the other is 4 bits representing the month (m). The “m” field [11:8] is the month code. 1 = January. The “y” field [19:12] is the year code. 0 = 2000. As an example, the binary value of the Date field for production date “April 2001” will be: 00000001 0100.
理解这并不难,难的部分是创建一个代码,根据十六进制数字放置日期时间。例如我收到 0x00 0xAC [000000000000000010101100] 我们只需要 12 位(前 4 位保留),这意味着 [000010101100],前 8 位是年份 0x0A = 10(DEC)= 2010,最后 4 位是 mounth [1100 ] = 12(DEC),所以这意味着我们有 12.2010 制造日期。如何用位进行这种操作,然后将结果放入 DateTime?我通过 serialport.readexisting 收到字符串表示形式的 HEX。
我们可以使用 BitArray()
将 2 个十六进制值转换为字节数组中的位表示形式。
然后,Math.Pow()
使用位位置将值转换为十进制表示形式。
new Date()
构造函数用于从 Year 和 Month 的值生成 DateTime 对象,然后转换为所需格式的字符串。
给定两个十六进制值 0x00
0xAC
:
Dim sHexValue1 As String = "0x00"
Dim sHexValue2 As String = "0xAC"
注意第一个字节设置为0xAC
的值,第二个设置为0x00
.
'Convert the two "Hex" strings to byte
Dim bytes As Byte() = {
Convert.ToByte(sHexValue2.Substring(2, 2), 16),
Convert.ToByte(sHexValue1.Substring(2, 2), 16)
}
'Convert the byte array in array of bits. => True means that the bit is on (1)
Dim bArray As New BitArray(bytes)
Dim month As Integer = 0
Dim year As Integer = 0
'Convert to decimal the value expressed by the first 4 bits
For x As Integer = 0 To 3
month += If(bArray(x) = True, CType(Math.Pow(2, x), Integer), 0)
Next
'Convert to decimal the value expressed by bits 4-12
For x As Integer = 0 To 7
year += If(bArray(x + 4) = True, CType(Math.Pow(2, x), Integer), 0)
Next
Dim resultDate = New Date(year + 2000, month, 1).ToString("MM.yyyy")