0

I want to convert my bytearray into binary like this ex: "01010101" . With small file, it can convert without any issues, but it's taking too long to convert a large file (even for just 5mb , i cant imagine for any larger than that ). is there any faster way to convert this bytearray into binary and vice versa ? here's my code

Public Function conv_FileToByte(ByVal filename As String)

    Dim convFileToByte_array() As Byte
    Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    Dim fileData As Byte() = New Byte(fs.Length - 1) {}

    Console.WriteLine("reading file data")
    fs.Read(fileData, 0, Convert.ToInt32(fs.Length))
    fs.Close()
    Console.WriteLine("close stream")


    convFileToByte_array = fileData

    Console.WriteLine("Returning value")

    Return convFileToByte_array

End Function


Public Function conv_ByteToBin(ByVal conv() As Byte)

    'Dim newBin As New List(Of String)
    Dim newBin As String = Nothing
    For Each c In conv
        'newBin.Add(Convert.ToString(c, 2).PadLeft(8, "0"))
        Dim temp_bin As String
        temp_bin = Convert.ToString(c, 2).PadLeft(8, "0")
        newBin = newBin & temp_bin
    Next
    Console.WriteLine("Returning value")

    Return newBin
End Function


Public Function conv_BinToByte(ByVal binValue As String)


    Dim count_binValue As String = binValue.Count

    Dim temp_binValue As New List(Of String)


    Dim bins As New Byte()
    Dim binlist As New List(Of Byte)

    For i As Integer = 0 To count_binValue - 1 Step 8


        Dim temp_value As String
        temp_value = binValue.Substring(i, 8)


        Dim convert_temp As String

        convert_temp = Convert.ToInt32(temp_value, 2)

        temp_binValue.Add(convert_temp)
    Next

    For Each bl In temp_binValue
        binlist.Add(bl)
    Next


    Dim binData As Byte() = New Byte(binlist.Count - 1) {}
    For bd As Integer = 0 To binlist.Count - 1
        binData(bd) = binlist(bd)
    Next

    Return binData

End Function
0

1 Answer 1

3

You are concatenating very large string in conv_ByteToBin-method. In such cases it's very bad practice to use basic string concatenating and it seems that this is your bottleneck here. I simply changed that method to use StringBuilder as it's an efficient way to concatenate large strings and the code run much, much faster:

Public Function conv_ByteToBin(ByVal conv() As Byte) As String
    Dim newBin As New StringBuilder

    For Each c In conv
        newBin.Append(Convert.ToString(c, 2).PadLeft(8, "0"))
    Next
    Console.WriteLine("Returning value")

    Return newBin.ToString
End Function

Best practices tips:

  • Always use return type in your methods
  • FileStream implements iDisposable - always use using block with objects that implement iDisposable

Also your method conv_FileToByte is irrelevant since .net already has built in File.ReadAllBytes-method that does the same thing. Just call that and remove your own implementation.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, this solved my problem ! I didn't know it was this simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.