Imports System.IO

Friend Class MyLib
    Public Class Encoding
        Public Shared Function LongHex(ByVal Number As Int64) As String
            Dim i, r As Int64
            Dim h As String = ""

            Do
                i = Int(Number / 16)
                r = Number - i * 16
                h = Hex$(r) + h
                Number = i
            Loop While Number > 0
            LongHex = h
        End Function

        'Convert the number to byte array (LSB in high byte) and save the result starting at startIndex
        Public Shared Function NumberToByteArray(ByVal Number As Int64, _
                                                 ByRef byteArray() As Byte, _
                                                 ByVal startIndex As Integer, _
                                                 ByVal Length As Integer) As Boolean
            Dim numberHex As String
            Dim i As Integer
            Dim hexLength As Integer

            If Length >= 1 And Length < 5 Then
                'numberHex = Right("00000000" & Hex(Number), 2 * Length)
                numberHex = Right("00000000" & LongHex(Number), 2 * Length)
                hexLength = Len(numberHex)
                For i = 1 To hexLength Step 2
                    byteArray(startIndex + (i - 1) / 2) = Val("&H" & Mid(numberHex, hexLength - i, 2))
                Next i

                NumberToByteArray = True
            Else
                Stop
                NumberToByteArray = False
            End If

        End Function


        Public Shared Function GetString(ByVal byteArray() As Byte)
            Return GetString(byteArray, 0, byteArray.Length)
        End Function

        Public Shared Function GetString(ByVal byteArray() As Byte, _
                                         ByVal startIndex As Integer, _
                                         ByVal length As Integer)
            Dim dataString As String = ""
            Dim i As Integer
            Dim endIndex As Integer

            Try
                endIndex = startIndex + length - 1

                For i = startIndex To endIndex
                    If byteArray(i) >= 20 And byteArray(i) <= 127 Then
                        dataString += Strings.Chr(byteArray(i))
                    Else
                        Exit For
                    End If
                Next

                Return dataString
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Function

        Public Shared Function GetBytes(ByVal stringData As String) As Byte()
            Return System.Text.ASCIIEncoding.Default.GetBytes(stringData)
        End Function





        'Treat as BCD, binary to decimal in long data type
        Public Shared Function ByteArrayToSignedNumber(ByRef byteArray() As Byte) As Object
            Dim convertedNumber As Int64 = 0
            Dim negativeNumber As Int64 = 0
            Dim tmp As Int64 = 0
            Dim i As Integer

            convertedNumber = 0

            If byteArray.Length > 8 Then
                Throw New Exception("Byte array cannot exceed than 8")
            End If

            For i = 0 To byteArray.Length - 1
                tmp = byteArray(i)
                tmp = tmp << (8 * i)
                convertedNumber += tmp
                'convertedNumber += byteArray(i) << (8 * i)
            Next

            Select Case byteArray.Length
                Case 1
                    If convertedNumber > SByte.MaxValue Then
                        'convert to negative number
                        negativeNumber = &H100
                        convertedNumber = CType(0 - (negativeNumber - convertedNumber), SByte)
                    Else
                        convertedNumber = CType(convertedNumber, SByte)
                    End If
                Case 2
                    If convertedNumber > Int16.MaxValue Then
                        'convert to negative number
                        negativeNumber = &H10000
                        convertedNumber = CType(0 - (negativeNumber - convertedNumber), Int16)
                    Else
                        convertedNumber = CType(convertedNumber, Int16)
                    End If
                Case 4
                    If convertedNumber > Int32.MaxValue Then
                        'convert to negative number
                        negativeNumber = &H100000000
                        convertedNumber = CType(0 - (negativeNumber - convertedNumber), Int32)
                    Else
                        convertedNumber = CType(convertedNumber, Int32)
                    End If
                Case Else
                    Stop
                    convertedNumber = CType(convertedNumber, Int64)
            End Select

            Return convertedNumber

        End Function


        Public Shared Function ByteToSByte(ByVal byteValue As Byte) As SByte
            Dim b(0) As Byte

            b(0) = byteValue

            Return ByteArrayToSignedNumber(b)
        End Function


        Public Shared Function ByteArrayToSignedNumber(ByRef byteArray() As Byte, _
                                                       ByVal startIndex As Integer, _
                                                       ByVal length As Integer) As Object
            Dim byteArrayX(length - 1) As Byte
            Dim convertedNumber As Int64 = 0

            Array.Copy(byteArray, startIndex, byteArrayX, 0, length)

            convertedNumber = ByteArrayToSignedNumber(byteArrayX)

            Return convertedNumber
        End Function


        'Treat as BCD, binary to decimal in long data type
        Public Shared Function ByteArrayToUnsignedNumber(ByRef byteArray() As Byte) As Object
            Dim convertedNumber As UInt64 = 0
            Dim negativeNumber As UInt64 = 0
            Dim tmp As UInt64 = 0
            Dim i As Integer

            convertedNumber = 0

            If byteArray.Length > 8 Then
                Throw New Exception("Byte array cannot exceed than 8")
            End If

            For i = 0 To byteArray.Length - 1
                tmp = byteArray(i)
                tmp = tmp << (8 * i)
                convertedNumber += tmp
                'convertedNumber += byteArray(i) << (8 * i)
            Next

            Select Case byteArray.Length
                Case 1
                    convertedNumber = CType(convertedNumber, Byte)
                Case 2
                    convertedNumber = CType(convertedNumber, UInt16)
                Case 4
                    convertedNumber = CType(convertedNumber, UInt32)
                Case Else
                    Stop
                    convertedNumber = CType(convertedNumber, UInt64)
            End Select

            Return convertedNumber

        End Function



        Public Shared Function ByteArrayToUnsignedNumber(ByRef byteArray() As Byte, _
                                                         ByVal startIndex As Integer, _
                                                         ByVal length As Integer) As Object
            Dim byteArrayX(length - 1) As Byte
            Dim convertedNumber As UInt64 = 0

            Array.Copy(byteArray, startIndex, byteArrayX, 0, length)

            convertedNumber = ByteArrayToUnsignedNumber(byteArrayX)

            Return convertedNumber
        End Function

        Public Shared Function ByteArrayToHexString(ByVal byteArray() As Byte) As String
            Dim i As Integer
            Dim hexString As String = ""

            For i = 0 To byteArray.Length - 1
                hexString += Hex(byteArray(i)).PadLeft(2, "0")
            Next

            Return hexString

        End Function


    End Class


End Class
