'===========================================================================
' Subject: EXTRACT/SET LOW & HIGH BYTES       Date: 06-07-91 (11:09)       
'  Author: Larry Stone                        Code: QB, QBasic, PDS        
'  Origin: QB TidBits                       Packet: ALGOR.ABC
'===========================================================================
'Code to extract (or set) low and high bytes from an integer by Larry Stone,
'June 7, 1991.
'
'Code to extract (or set) low and high integers from a LONG derived from code
'published in the June, 1991, issue of "Inside QuickBASIC" by Doug Quebbeman.

DEFINT A-Z                      'Default to integers

'****************************************************************************
'                              TYPE Declarations                            *
'****************************************************************************

TYPE Integr                     'Create a template (structure), Integr
    Num AS INTEGER              '(only has 1 element)
END TYPE

TYPE SplitInt                   'Create a template (structure), SplitInt
    LowByte AS STRING * 1
    HiByte  AS STRING * 1
END TYPE

TYPE Longe                      'Create a template (structure), Longe
    Dword AS LONG               '(only has 1 element)
END TYPE

TYPE SplitLong                  'Create a template (structure), SplitLong
    LowWord AS INTEGER
    HiWord AS INTEGER
END TYPE

'****************************************************************************
'                 DIM memory variables as Structured TYPEs                  *
'****************************************************************************

DIM Integr AS Integr            'Create our memory variable, Integr
DIM SplitInt AS SplitInt        'Create our memory variable, SplitInt
DIM Longe AS Longe              'Create our memory variable, Longe
DIM SplitLong AS SplitLong      'Create our memory variable, SplitLong

'****************************************************************************
'                          Make It Work - Main Code                         *
'****************************************************************************

CLS                             'Clear the screen

Integr.Num = 32767
PRINT "Our INTEGER"; Integr.Num;

LSET SplitInt = Integr          'Get low and high bytes of our Integr
PRINT "has a low byte of"; ASC(SplitInt.LowByte);
PRINT "and a high byte of"; ASC(SplitInt.HiByte)

SplitInt.HiByte = CHR$(128)     'Assign a new high byte for our Integr
PRINT "Setting the high byte of the INTEGER to"; ASC(SplitInt.HiByte);

LSET Integr = SplitInt          'Set the Integr to these new byte values
PRINT "changes it's value to "; Integr.Num
PRINT


Longe.Dword = 131071
PRINT "Our LONG Integer"; Longe.Dword;

LSET SplitLong = Longe          'Get the low and high integers of our Longe
PRINT "has a low integer of "; SplitLong.LowWord;
PRINT "and a high integer of"; SplitLong.HiWord

Integr.Num = SplitLong.LowWord  'Assign Integr as the LowWord
LSET SplitInt = Integr          'Now get the bytes of our LowWord

PRINT "The low integer's ( "; Integr.Num; ") low byte is";
PRINT ASC(SplitInt.LowByte);
PRINT "and its high byte is"; ASC(SplitInt.HiByte)

Integr.Num = SplitLong.HiWord   'Assign Integr as the HiWord
LSET SplitInt = Integr          'Now get the low and high bytes of HiWord

PRINT "The high integer's ("; Integr.Num; ") low byte is";
PRINT ASC(SplitInt.LowByte);
PRINT "and its high byte is"; ASC(SplitInt.HiByte)
PRINT

SplitLong.HiWord = 32767        'Change the high INTEGER of our variable

PRINT "Assigning"; SplitLong.HiWord; "as our LONG's high word ";
LSET Longe = SplitLong          'Now assign the above integers to Longe

PRINT "changes its value to"; Longe.Dword
PRINT "The low integer of"; Longe.Dword; "reports as "; SplitLong.LowWord;
PRINT "and the high integer is"; SplitLong.HiWord
