none
Extracting Manufacture date from Hex RRS feed

  • Question

  • Im working with SD card, succesfuly reading SID from ic to vb.net.Problem comes when i need a code for Manufacture date. Datasheet says:

    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.

    Understing this wasnt hard, the hard part is to create a code which will put DateTime according to hex numbers. For example i recevied 0x00 0xAC [000000000000000010101100] we take only 12bits(first 4 is reserved), it means [000010101100], first 8 bits is a year 0x0A = 10(DEC) = 2010, and last 4 bits its a mounth [1100] = 12(DEC), so it means we have 12.2010 Manufacture date. How to do this kind of manipulation with bits then put result into DateTime? I receive HEX in string representation through serialport.readexisting.

    Monday, January 22, 2018 8:53 AM

All replies

  • I receive HEX in string representation through serialport.readexisting.

    If that input data really is a hex string then you need to first convert to integer.  Then you can extract the relevant part of each value.  Note that if you want to create a date then you must assume a day.  The time will default to zero.

            Dim S1 As String = "01"
            Dim S2 As String = "4c"
    
            Dim B1 As Integer = Convert.ToInt32(S1, 16)
            Dim B2 As Integer = Convert.ToInt32(S2, 16)
    
            Dim Year As Integer = B1 + 2000
            Dim Month As Integer = B2 >> 4
            Dim D As Date = New Date(Year, Month, 1)

    Monday, January 22, 2018 9:53 AM
  • This one took me some time.  However, I would not use a datetime for this. A datetime is a tick in an Int64 which is with properties translated to all kind of representations. 

    I would create my own structure. 

    Your code is than like this. 

    Module Module1
        Sub Main()
            Dim MD As ManDate
            MD.Hex = &HAC 'VB hex notation
            Console.WriteLine(MD.Year)
            Console.WriteLine(MD.Month)
            Console.ReadLine()
        End Sub
    End Module
    Public Structure ManDate
        Private _value As Byte
        Public WriteOnly Property Hex As Byte
            Set(value As Byte)
                _value = value
            End Set
        End Property
        Public ReadOnly Property Month As Integer
            Get
                Return _value And &HF
            End Get
        End Property
        Public ReadOnly Property Year As Integer
            Get
                Return _value >> 4
            End Get
        End Property
    End Structure


    Success Cor


    Monday, January 22, 2018 6:53 PM
  • Maybe show an example of the received hexadecimal string. Since the date field of CID has 12 bits, you will need to extract three hexadecimal digits. The further calculation depends on the order of these digits in your strings.


    • Edited by Viorel_MVP Monday, January 22, 2018 8:22 PM
    Monday, January 22, 2018 8:21 PM
  •     Dim S1 As String = "0A"
                Dim S2 As String = "0C"

                Dim B1 As Integer = Convert.ToInt32(S1, 16)
                Dim B2 As Integer = Convert.ToInt32(S2, 16)

                Dim Year As Integer = B1 + 2000
                Dim Month As Integer = B2 >> 4
                Dim D As Date = New Date(Year, Month, 1)
                TextBox1.Text &= "      Manufacturing date....: " & D.ToString

    Drops me an exception 

    Tuesday, January 23, 2018 2:38 AM
  • didnt get the code =( 

    why 

      MD.Hex = &HAC 'VB hex notation

    Only 1 hex char? We are using 2 

    Tuesday, January 23, 2018 2:40 AM
  • Drops me an exception 

    You have changed the code.  According to your description '0c' would be an illegal value - month number must be from 1 to 12.

    If '0c' is a value that you are receiving then you should indicate how that gets translated to a month number.  Should the code use the low 4-bits of that value, not the high 4-bits? 

    Tuesday, January 23, 2018 3:23 AM
  • Only 1 hex char? We are using 2 

    The problem is that there is no such thing as a hexadecimal digit.  Each character in a hex string represents 4 bits.  They usually come in 8- or 16-bit combinations, so the hex characters appear in multiples of 2.   If you are using 2 'hex char' then you have 8 bits to work with - not enough.  If you mean two hex pairs (as per your example - 0x00 0xAC) then you have 16 bits.  Of that 16 you are using 12.  

    So, either you have three hex chars, representing 12 bits - yyyyyyyymmm.

    Or you have two hex pairs, representing 16 bits, of which you are using 12.  That can be either yyyyyyyy mmmm???? or yyyyyyyy ????mmmm. In each case mmmm cannot be 0000.  Other combinations are possible, of course, but I think it will be one of the above.

    Tuesday, January 23, 2018 3:30 AM
  • didnt get the code =( 

    why 

      MD.Hex = &HAC 'VB hex notation

    Only 1 hex char? We are using 2 

    In your sample you use C notation. In VB 0000000010101100 is represented by &H00AC for Hex 00AC, however visual studio makes automatically &HAC from this. 

    Strange enough everybody else is going on in this C# way of describing in this VB forum.


    Success Cor



    Tuesday, January 23, 2018 6:00 AM