Q on integer, little endian, LSet, data formats, ..

D

Dave D-C

Option Explicit

' (this must be a standard module)
' I am moveing , e.g., x8C01 (chr$(140) & chr$(1)) into an integer
field.
' I do this by first moving that string into a string field,
' then using LSet to move the string field into the integer field.
' Considering little endian, I expect the integer to be x018C=396.
' Instead, I get 338 (see output row 141). What am I missing?
'Also, see rows 129, 131, etc. I am using Excel97
' Thanks, Dave

Type typI2
i2 As Integer
End Type

Type typS2
s2 As String * 2
End Type

Sub Sub1() ' demonstrate my issue
' Look at rows 129, 131, 141, etc. on output
' Why aren't these as I expect them to be?
Dim recI2 As typI2, recS2 As typS2
Dim i1%, i2%, irow&
i2 = 1 ' this doesn't seem to have any effect
For i1 = 0 To 255
recS2.s2 = Chr$(i1) & Chr$(i2)
LSet recI2 = recS2 ' binary move s2 to i2
irow = irow + 1
Cells(irow, 1) = i1
Cells(irow, 2) = i2
Cells(irow, 3) = recI2.i2
Next i1
End Sub
 
T

Tom Ogilvy

Dave,
You problem stems from unicode. Strings in VBA are stored internally as
unicode (16 bits). This modification to your code should give you some
insights. The problematic rows you speak of are being mapped to characters
that you don't expect.

Type typI2
i2 As Integer
End Type

Type typS2
s2 As String * 2
End Type

Sub Sub1() ' demonstrate my issue
' Look at rows 129, 131, 141, etc. on output
' Why aren't these as I expect them to be?
Dim recI2 As typI2, recS2 As typS2
Dim b() As Byte, i as Long
Dim i1%, i2%, irow&
i2 = 0 ' this doesn't seem to have any effect
For i1 = 0 To 255
recS2.s2 = Chr$(i1) & Chr$(i2)
b = Chr$(i1) & Chr$(i2)
LSet recI2 = recS2 ' binary move s2 to i2
irow = irow + 1
Cells(irow, 1) = i1
Cells(irow, 2) = i2
Cells(irow, 3) = recI2.i2
For i = LBound(b) To UBound(b)
Cells(irow, i + 4) = b(i)
Next
Next i1
End Sub
 
D

Dave D-C

For anybody who looked at this, I searched more and found posts by
Larry Serflaten to be invaluable. It seems maybe somehow (?) strings
are not consecutive bytes, so use arrays of byte instead.

I've made changes noted in [ ] s:
Option Explicit

' (this must be a standard module)
' I am moveing , e.g., x8C01 (chr$(140) & chr$(1)) into an integer
field.
' I do this by first moving that string into a string field,
' then using LSet to move the string field into the integer field.
' Considering little endian, I expect the integer to be x018C=396.
' Instead, I get 338 (see output row 141). What am I missing?
'Also, see rows 129, 131, etc. I am using Excel97
' Thanks, Dave

Type typI2
i2 As Integer
End Type

Type typS2
s2 As String * 2 [delete this line]
[ s2(1) as byte ' really 2 bytes of course]
End Type

Sub Sub1() ' demonstrate my issue
' Look at rows 129, 131, 141, etc. on output
' Why aren't these as I expect them to be?
Dim recI2 As typI2, recS2 As typS2
Dim i1%, i2%, irow&
i2 = 1 ' this doesn't seem to have any effect
For i1 = 0 To 255
recS2.s2 = Chr$(i1) & Chr$(i2) [delete this line]
[ recs2.s2(0) = Asc(Chr$(i1)) ]
[ recs2.s2(1) = Asc(Chr$(i2)) ]
 
T

Tom Ogilvy

I gave you the correct answer 3 days ago - less than two hours after you
asked. I doubt anyone was wasting there time worrying about it.

--
Regards,
Tom Ogilvy

Dave D-C said:
For anybody who looked at this, I searched more and found posts by
Larry Serflaten to be invaluable. It seems maybe somehow (?) strings
are not consecutive bytes, so use arrays of byte instead.

I've made changes noted in [ ] s:
Option Explicit

' (this must be a standard module)
' I am moveing , e.g., x8C01 (chr$(140) & chr$(1)) into an integer
field.
' I do this by first moving that string into a string field,
' then using LSet to move the string field into the integer field.
' Considering little endian, I expect the integer to be x018C=396.
' Instead, I get 338 (see output row 141). What am I missing?
'Also, see rows 129, 131, etc. I am using Excel97
' Thanks, Dave

Type typI2
i2 As Integer
End Type

Type typS2
s2 As String * 2 [delete this line]
[ s2(1) as byte ' really 2 bytes of course]
End Type

Sub Sub1() ' demonstrate my issue
' Look at rows 129, 131, 141, etc. on output
' Why aren't these as I expect them to be?
Dim recI2 As typI2, recS2 As typS2
Dim i1%, i2%, irow&
i2 = 1 ' this doesn't seem to have any effect
For i1 = 0 To 255
recS2.s2 = Chr$(i1) & Chr$(i2) [delete this line]
[ recs2.s2(0) = Asc(Chr$(i1)) ]
[ recs2.s2(1) = Asc(Chr$(i2)) ]
LSet recI2 = recS2 ' binary move s2 to i2
irow = irow + 1
Cells(irow, 1) = i1
Cells(irow, 2) = i2
Cells(irow, 3) = recI2.i2
Next i1
End Sub
 
D

Dave D-C

I've got to increase my message number limit or check the newsgroup
more often. (This newsgroup is BUSY!) Somehow I missed your first
response. I see it now on google. Thanks for your great info. Dave

Tom Ogilvy said:
I gave you the correct answer 3 days ago - less than two hours after you
asked. I doubt anyone was wasting there time worrying about it.

Tom wrote (earlier):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top