locked
How to retrieve/replace the Char Array in a class from database (SQL) RRS feed

  • Question

  • User600329884 posted

    Hi master, please help me. I have a class there a char base64 index. so I want to dynamic from database. Here this class :

    Imports System.Data
    Imports System.Data.SqlClient
    Namespace TestBase64
        ''' <summary>
        ''' Summary description for Base64Encoder.
        ''' </summary>
        Public Class Base64Encoder
            Private source As Byte()
            Private length As Integer, length2 As Integer
            Private blockCount As Integer
            Private paddingCount As Integer
    
         
            Public Sub New(input As Byte())
                source = input
                length = input.Length
                If (length Mod 3) = 0 Then
                    paddingCount = 0
                    blockCount = length \ 3
                Else
                    paddingCount = 3 - (length Mod 3)
                    'need to add padding
                    blockCount = (length + paddingCount) \ 3
                End If
                'or blockCount *3
                length2 = length + paddingCount
            End Sub
    
            Public Function GetEncoded() As Char()
                Dim source2 As Byte()
                source2 = New Byte(length2 - 1) {}
                'copy data over insert padding
                For x As Integer = 0 To length2 - 1
                    If x < length Then
                        source2(x) = source(x)
                    Else
                        source2(x) = 0
                    End If
                Next
    
                Dim b1 As Byte, b2 As Byte, b3 As Byte
                Dim temp As Byte, temp1 As Byte, temp2 As Byte, temp3 As Byte, temp4 As Byte
                Dim buffer As Byte() = New Byte(blockCount * 4 - 1) {}
                Dim result As Char() = New Char(blockCount * 4 - 1) {}
                For x As Integer = 0 To blockCount - 1
                    b1 = source2(x * 3)
                    b2 = source2(x * 3 + 1)
                    b3 = source2(x * 3 + 2)
    
                    temp1 = CByte((b1 And 252) >> 2)
                    'first
                    temp = CByte((b1 And 3) << 4)
                    temp2 = CByte((b2 And 240) >> 4)
                    temp2 += temp
                    'second
                    temp = CByte((b2 And 15) << 2)
                    temp3 = CByte((b3 And 192) >> 6)
                    temp3 += temp
                    'third
                    temp4 = CByte(b3 And 63)
                    'fourth
                    buffer(x * 4) = temp1
                    buffer(x * 4 + 1) = temp2
                    buffer(x * 4 + 2) = temp3
    
                    buffer(x * 4 + 3) = temp4
                Next
    
                For x As Integer = 0 To blockCount * 4 - 1
                    result(x) = sixbit2char(buffer(x))
                Next
    
                'covert last "A"s to "=", based on paddingCount
                Select Case paddingCount
                    Case 0
                        Exit Select
                    Case 1
                        result(blockCount * 4 - 1) = "="c
                        Exit Select
                    Case 2
                        result(blockCount * 4 - 1) = "="c
                        result(blockCount * 4 - 2) = "="c
                        Exit Select
                    Case Else
                        Exit Select
                End Select
                Return result
            End Function
    
            Private Function sixbit2char(b As Byte) As Char
                Dim lookupTable As Char() = New Char(63) {"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, _
                    "G"c, "H"c, "I"c, "J"c, "K"c, "L"c, _
                    "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c, _
                    "S"c, "T"c, "U"c, "V"c, "W"c, "X"c, _
                    "Y"c, "Z"c, "a"c, "b"c, "c"c, "d"c, _
                    "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, _
                    "k"c, "l"c, "m"c, "n"c, "o"c, "p"c, _
                    "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, _
                    "w"c, "x"c, "y"c, "z"c, "0"c, "1"c, _
                    "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
                    "8"c, "9"c, "+"c, "/"c}
    
                If (b >= 0) AndAlso (b <= 63) Then
                    Return lookupTable(CInt(b))
                Else
                    'should not happen;
                    Return " "c
                End If
            End Function
        End Class
    End Namespace

    The questions is how to retrieve or replace this code (char array) from database (sql) as example

            Dim lookupTable As Char() = New Char(63) {"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, _
                    "G"c, "H"c, "I"c, "J"c, "K"c, "L"c, _
                    "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c, _
                    "S"c, "T"c, "U"c, "V"c, "W"c, "X"c, _
                    "Y"c, "Z"c, "a"c, "b"c, "c"c, "d"c, _
                    "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, _
                    "k"c, "l"c, "m"c, "n"c, "o"c, "p"c, _
                    "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, _
                    "w"c, "x"c, "y"c, "z"c, "0"c, "1"c, _
                    "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, _
                    "8"c, "9"c, "+"c, "/"c}

    Please Help me,

    Very Hope your answer and solutions.

    Thank You, Master

    Tuesday, January 7, 2014 8:34 PM

Answers

  • User-417640953 posted

    Hi Mafullah,

    From your class Base64Encoder, I see the method GetEncoded can convert the byte array to the chart array.

    Then if you want to encode the data comes from database. I think you should read the data from database with the type Byte().

    As my knowledge, sql server data type "binary" corresponding the type vb.net "Byte()". For this issue, you just read the Byte() data from database

    to your application, then you can convert it like below.

     Base64Encoder bec = new Base64Encoder(byteFromDatabase);
     char[] mychar= bec.GetEncoded();

    If I misunderstand your issue, please feel free to back. Thanks.

    Best Regards!

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 2:23 AM