Union Data type in VB?? 
Author Message
 Union Data type in VB??

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);

Quote:
}

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



Tue, 10 Aug 1999 03:00:00 GMT  
 Union Data type in VB??



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

Quote:

>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




Tue, 10 Aug 1999 03:00:00 GMT  
 Union Data type in VB??

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




Wed, 11 Aug 1999 03:00:00 GMT  
 Union Data type in VB??



Quote:
> 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.

Thanks for the help

The Byte data type is a single unsigned byte.  I think I must be missing
something but if I have
and array of 8 bytes how do I combine them into a double?  I always seem to
get either 0 or the values of the bytes added together as if they were
strings.

ie. these value should produce 15.2346 but I get 12872191..... etc., or 0
or 775

   bByte(0) = 128
   bByte(1) = 72
   bByte(2) = 191
   bByte(3) = 125
   bByte(4) = 29
   bByte(5) = 120
   bByte(6) = 46
   bByte(7) = 64

 I do get the correct result if I write the bytes to a binary file and then
read a double but this seems cumbersome.



Wed, 11 Aug 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Passing of C language's union data type in VB application

2. Does VB a similar data type like the C++ Union type

3. Does VB a similar data type like the C++ Union type

4. problem of converting C's union type to VB's a data type

5. problem of converting C's union type to VB's a data type

6. What's equiv. of C union data type for VB?

7. Declaring C-Language Union and Bit-field data types in Visual Basic

8. Declaring C-Language Union and Bit-field data types in Visual Basic

9. C data type vs. VB Data type

10. VB Data Types - C Data Types

11. Does anyone know how to implement UNIONS in VB types

12. Get error: Disallowed implicit conversion from data type varchar to data type money

 

 
Powered by phpBB® Forum Software