Need Binary conversion from a blob of 2 byte binary data

Posted on 2006-06-06
Last Modified: 2010-04-07
I did this before and forgot how I did it

The problem that I am having is I am pulling data from a SQL anywhere database and need to convert a Binary group of values to integers. The values are showing up as "?????????????????????????????????" this is a group of two byte binary integers that I need to pull from this data How do I extract a 2 byte binary integer.  
Question by:dsulkar
    LVL 13

    Accepted Solution

    Hi  dsulkar
    First of all the data is showing this way because you are not displaying it as unicode (the default VB string encoding is Unicode) where unicode represents each character with two bytes, so whats happening is that VB is considering every two characters of your string as on unicode character and you get the previous output.

    To solve this you need to convert the pulled out string to unicode:

    Dim tmpStr As String
    tmpStr = StrConv(YourBlobText, vbUnicode)

    But what I Understood from your question is that you want to convert binary data to integers and that is a different issue. In this case you shouldn't convert the data to unicode.
    If you've read the data into a byte array or in to a string variable then the following code will convert the data into an array of integers:

    ' Add a command Button to a form and paste the following code:

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

    Dim IntAr() As Integer

    Private Sub BytetoArray(Data() As Byte)
    Dim tmpNum As Long

    ' Calculate the number of Byte items in our Byte array
    tmpNum = (UBound(Data) - LBound(Data) + 1)

    ' Redimention the Integer array to fit the items
    ReDim IntAr(tmpNum / 2 - 1)

    CopyMemory IntAr(0), Data(0), tmpNum
    End Sub

    Private Sub Command1_Click()
    Dim tmpAr() As Byte

    ' Convert string to byte array
    tmpAr = StrConv("dfgdfg", vbFromUnicode) ' no need for you to do this because you already have your string in a nonunicode format
    ' Fill in the integer array
    ' if you have a byte array you can pass it immediately
    BytetoArray tmpAr

    For i = 0 To UBound(IntAr)
        Print CStr(IntAr(i))
    End Sub
    LVL 6

    Author Comment

    Just got in, thanks for the responce I will try it and get back to you.
    LVL 6

    Author Comment

    Thanks very much, you got me on the right track and Ireally appreciate the code sample and explination.
    Great Job
    LVL 13

    Expert Comment

    Your welcome

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now