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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Converting Long to Byte 1

Status
Not open for further replies.

Bluejay07

Programmer
Mar 9, 2007
780
CA
In VB6, how do you convert long and integer values into a single byte.

For example, if I use the string
11913ACE215Ace Electronics
Where 119 = length,
1 = index,
3 = lenth of data to follow
ACE = Data
2 = index
15 = length of data
...

To produce the format:

[BYTE][BYTE][BYTE]ACE[BYTE][BYTE]Ace Electronics
similar to (not sure if this will show)
DataData

I have tried a methods such as
1)
Code:
   Dim myFSO As New FileSystemObject
   Dim OS As TextStream
   Dim Str As String, by() As Byte

   Str = "17Cust-01"
   by = StrConv(Str, vbFromUnicode)

   Set OS = myFSO.CreateTextFile("C:\Projects\Test.DAT")
   For i = 1 To UBound(by)
      OS.Write by(i)
   Next i
2)
Code:
   Dim Str As String
   Dim CharArray() As Byte
   Dim i As Integer

   Str = "My test string"
   ReDim CharArray(Len(Str))
   For i = 1 To Len(Str)
      CharArray(i) = Asc(Mid$(Str, i, 1))
   Next i

However, Neither of these methods seem to make any difference to the output.

I think I might be missing something. My current output still shows the numberic values (11913...), however I need to display a single byte for each numeric value.
 
Not sure what you're after here (your example didn't render well); integers are stored as two bytes, longs as four.
If the numbers are sufficiently small you might be able to covert using the CByte() function, but I think you get an error about scalabilty or something.....

I wondered why the baseball was getting bigger.
Then it hit me.
 
1 Byte = 8 Bits and 1 bit = 1 or zero and 1 or zero = binary

So I am thinking you must be after a binary conversion.


8 bits of data = 00000000
the byte version of the number 1 is 00000001

each of these 8 bit locations from right to left increase by a multiple of 2 starting with 1
so 0x128 0x64 0x32 0x16 0x8 0x4 0x2 0x1

so if we plug the number 1 into the 7th location from the left this is the Byte representation
of the decimal 2.
If you plug a 1 into all 8 locations you can see that the largest value you can represent
with one byte is 255. a little off topic but this is why Red Green Blue (RGB) values are
maximum of 255 each with 255,0,0 representing the red at its most red.

assuming you want a byte conversion for each digit I modified
some code from Francesco Balena at devx.com to do just that.


Add a command button and a textbox to a form and paste the code for testing.

Code:
Private Sub Command1_Click()
    MsgBox byte_converter(Text1.Text)
End Sub

Public Function byte_converter(stringtoconvert As String) As String
Dim newstring As String
Dim portionStr As String
Dim x As Integer
'break the string into single units
loopstr = Len(stringtoconvert)

For x = 1 To Len(stringtoconvert)
    'store values in array
    portionStr = Mid(stringtoconvert, x, 1)
    If IsNumeric(portionStr) Then
        'value is a number so convert to byte
        newstring = newstring & CStr(Bin(portionStr))
    Else
        'value is not a number add it to string
        newstring = newstring & portionStr
    End If
Next x
byte_converter = newstring
End Function

Public Function Bin(ByVal value As Long, Optional digits As Long = 8) As String
    Dim result As String, exponent As Integer
    ' this is faster than creating the string by appending chars
    result = String$(32, "0")
    Do
        If value And Power2(exponent) Then
            ' we found a bit that is set, clear it
            Mid$(result, 32 - exponent, 1) = "1"
            value = value Xor Power2(exponent)
        End If
        exponent = exponent + 1
    Loop While value
    If digits < 0 Then
        ' trim non significant digits, if digits was omitted or negative
        Bin = Mid$(result, 33 - exponent)
    Else
        ' else trim to the requested number of digits
        Bin = Right$(result, digits)
    End If
End Function

Public Function Power2(ByVal exponent As Long) As Long
    Static res(0 To 31) As Long
    Dim i As Long
    
    ' rule out errors
    If exponent < 0 Or exponent > 31 Then Err.Raise 5
    
    ' initialize the array at the first call
    If res(0) = 0 Then
        res(0) = 1
        For i = 1 To 30
            res(i) = res(i - 1) * 2
        Next
        ' this is a special case
        res(31) = &H80000000
    End If
    
    ' return the result
    Power2 = res(exponent)
        
End Function

Private Sub Form_Load()
    Text1.Text = "11913ACE215Ace Electronics"
End Sub



 
Converting to bytes is actually pretty trivial (ByteVal=CByte(numeric_string) where numeric_string<=255). The difficult bit is how to identify what goes in which byte.

Using your own example, if you get a string

11913ACE215Ace Electronics

How do you determine that its

119 = length,
1 = index
3 = length of data to follow

and not

11 = length
9 = index
13 = length of data to follow
 
>'store values in array
by the way ignore this comment in the code I was going to
store to array but didnt just forgot to delete the comment.
 
I thank you all for your help and comments. It is much appreciated.

I guess I still need to learn more about VB.

The problem is that VB6 doesn't support bit shifting. three57m solution does help me a bit with bit shifting examples.

The CBYTE() function seems to work alright, but now if I have a value > 255, how do you store this value in two bytes.

 
OP said:
I think I might be missing something.
What's missing is why in the world are you trying to do this? It looks like an ancient approach to data transmission. The answers here might be better if you told us what you were trying to do, rather than a small bit of how you were trying to do it.
 

I am reading records that were placed in a UDT that are all string values. This information is being used to create a single output string, as small as possible, as the final string will be read by a BlackBerry program.

I need to display the information in the following form:
[RECORD LENGTH 2 BYTES][FIELD TYPE-1 BYTE][FIELD LENGTH-1 BYTE]....data....[FIELD TYPE-1 BYTE][FIELD LENGTH-1 BYTE]....data....(and this form repeats until EOF)

The field type (ID field) is a hardcoded integer value (I am currently using CByte() for this value before adding it to the string) and the field length is a long value obtained by the Len(str) function.

My inexperience in this area had lead to problems of data conversion. I appologize if I have been unclear in addressing this issue.
 
>but now if I have a value > 255, how do you store this value in two bytes

CInt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top