Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Need Binary conversion from a blob of 2 byte binary data

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.  
0
dsulkar
Asked:
dsulkar
  • 2
  • 2
1 Solution
 
iHadiCommented:
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))
Next
End Sub
0
 
dsulkarAuthor Commented:
Just got in, thanks for the responce I will try it and get back to you.
Diven
0
 
dsulkarAuthor Commented:
Thanks very much, you got me on the right track and Ireally appreciate the code sample and explination.
Great Job
Diven
0
 
iHadiCommented:
Your welcome
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now