Not exactly true. A variant is actually implemented as a struct - the
first member is indicates the type (vt), and the second member is a
union of a bunch of different types. But this is not what you want.
I think you can do this using bitwise operators. For example
Dim foo As Integer
Dim bar As Integer
foo = 32000
bar = foo And &HFF00&
Debug.Print "foo : " & foo & " bar : " & bar
This is a bit mask. At this point, bar holds the upper byte. To move
that data to the lower byte,- subtract 2^8. You can use this bit mask
and shift to do what you are suggesting. Of course you can mask and
shift the low byte also. This is a handy trick but, you will note that
I haven't taken into account the sign bit. This limits the size of
your data and will probably hose your conversion.
I don't see an unsigned data type in VB - suck. You can try the
following: Declare a user defined type of array of eight bytes. Read
the data into your new type. Once you get the data in the type, you
can pretty easily pull it out using array indices. This may take a bit
of massaging and you may get to use the bit mask and shift above any
way.
Quote:
>Not in VB4.0 and before (I don't know about VB 5.0). Union is a shared piece of
>memory. There is no structure in native VB to share memory among different
>variables. The closest you can get is the VARIANT typeless data type, but that
>would be a poor substitute at best.
>Greg
>>Can you create or emulate Union data type in VB like in C?
>>I need to read a binary file created by an Atari computer. The problem is
>>that the Atari stores the 8 bytes that make up a Double in the reverse
>>order form the IBM. I did this in some C code that used a union of 8 chars
>>and a double. The C code to read a double looks like this:
>>double GetAtariDouble(FILE *fp)
>>{
>> union number {
>> double Double;
>> char Char[8];
>> };
>> union number Number;
>> int i;
>> for(i=7; i>=0; i--)
>> fread(&Number.Char[i], 1, 1, fp);
>> return(Number.Double);
>>}
>>Can anyone figure out a way to do this in VB? I realize I could put this
>>in a DLL but as I've never made a DLL before I'd like to avoid that path
>>for now.
>>Any help is appreciated.
>>David Gunstensen