Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting binary to ascii text

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
In order to use teh instr function over a large binary file, I convert it to text using:
Code:
' Convert Binary to ASCII
Function BinaryToString(Binary)
  dim S
  For I = 1 To LenB(Binary)
    S = S & Chr(AscB(MidB(Binary, I, 1)))
  Next
  BinaryToString = S
end function

It's incredibly slow, even for 1Mb files which I don't consider large.

Is there a faster way please?!

thanks


Applications Support
UK
 
Yep, just did.. did not convert binary stream.

thanks


Applications Support
UK
 
Hi again,

It seems cstr does not work at all for bin to string, I tried it at stream and single byte level

sorry!



Applications Support
UK
 
Tsuji,

thanks, that's where I took the original from.

Using the enhanced version did not work... or at least, did not appear to. Perhaps I shoudkl try again!

Thanks


Applications Support
UK
 
oh, step 2 DID work and very quickly.

Thanks for assisting me to re-look!

thanks


Applications Support
UK
 
MCubitt, that's reassuring. Thanks. - tsuji
 
Well that improved version still uses string concatination so if the file gets big enough then your performance problem will return.

An alternate approach would be to use an array. This method still converts the characters one at a time but it puts each one into an element of an array. Once all of the characters are converted, the Join() function is used on the array.

Code:
Function MagicArray(Binary)
    Dim L
    Dim BigArray()
    Dim Counter
    
    L = LenB(Binary)
    ReDim BigArray(L)
    
    For Counter = 1 To L
        BigArray(Counter - 1) = Chr(AscB(MidB(Binary, Counter, 1)))
    Next
    
    MagicArray = Join(BigArray, "")
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top