How can I convert a 16-bit number into 2 8-bit numbers
Author Message
How can I convert a 16-bit number into 2 8-bit numbers

How can I split an integer into 2 addresses (high and low bytes)

I have the code of how to do it in C, but I'm having trouble converting it
to VB :

(Base is an int)

WBlock[0] = ( Base >> 8 ) & 0xFF ;
WBlock[1] = Base & 0xFF ;

Could you possibly mail me due to problems with reading news.

Rich

Mon, 26 Apr 1999 03:00:00 GMT
How can I convert a 16-bit number into 2 8-bit numbers

Quote:

> How can I split an integer into 2 addresses (high and low bytes)
> (Base is an int)

>     WBlock[0] = ( Base >> 8 ) & 0xFF ;
>     WBlock[1] = Base & 0xFF ;

> Rich

Here's two functions that should do the trick.

FUNCTION HByte% (word%)
IF word% >= 0 THEN
HByte% = word% \ 256
ELSE
HByte% = (65536 + word%) \ 256
END IF
END FUNCTION

FUNCTION LByte% (word%)
IF word% >= 0 THEN
LByte% = word% MOD 256
ELSE
LByte% = (65536 + word%) MOD 256
END IF
END FUNCTION

Of course, if you know that you'll only be dealing with positive integers
less than 32767 (like a positive signed int in C) just use MOD or '\'
(integer divide). So in order to get the lowbyte value of 23456 just say

PRINT lbyte(23456)

or

PRINT 23456 MOD 256

Mon, 26 Apr 1999 03:00:00 GMT
How can I convert a 16-bit number into 2 8-bit numbers

Quote:

>How can I split an integer into 2 addresses (high and low bytes)

>I have the code of how to do it in C, but I'm having trouble convertin
>g it
>to VB :

>(Base is an int)

>    WBlock[0] = ( Base >> 8 ) & 0xFF ;
>    WBlock[1] = Base & 0xFF ;

>Could you possibly mail me due to problems with reading news.

>Rich

Word = 1234     '(0-65535)

Byte0 = Word AND &HFF
Byte1 = (Word AND &HFF00) \ &H100

...or

Byte0 = ASC(MID\$(MKI\$(Word), 1, 1))
Byte1 = ASC(MID\$(MKI\$(Word), 2, 1))

Beau Schwabe

Tue, 27 Apr 1999 03:00:00 GMT
How can I convert a 16-bit number into 2 8-bit numbers

Quote:

>How can I split an integer into 2 addresses (high and low bytes)

Hi RH!

Lotsa different technques I see! here;s my two cents on how I do it:

Number = 32767
MSB = Number \ 256      'backslash = integer div
LSB = Number MOD 256

Works for me! Ep

Ed Parry - Southern California, USA

Tue, 27 Apr 1999 03:00:00 GMT
How can I convert a 16-bit number into 2 8-bit numbers

: How can I split an integer into 2 addresses (high and low bytes)
:
: I have the code of how to do it in C, but I'm having trouble converting it
: to VB :
:
: (Base is an int)
:
:     WBlock[0] = ( Base >> 8 ) & 0xFF ;
:     WBlock[1] = Base & 0xFF ;
:
: Could you possibly mail me due to problems with reading news.
:
:
: Rich

:

Tue, 27 Apr 1999 03:00:00 GMT
How can I convert a 16-bit number into 2 8-bit numbers

RI>I have the code of how to do it in C, but I'm having trouble converting it
RI>to VB :

I don't know VB but:

low = Value AND 255
high = (value AND -256)/256

Wed, 28 Apr 1999 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages