[Gambas-user] 16 bit value in hex with MSB and LSB?
Tobias Boege
taboege at ...626...
Thu May 18 11:55:07 CEST 2017
On Thu, 18 May 2017, alexchernoff wrote:
> Good day all,
>
> In a serial string I receive a 16 bit value split into MSB and LSB in hex,
> for example 15000 (3A 98) is sent as 2 bytes containing decimal values of
> 3A and 98.
>
> To convert that to decimal, I have to convert each to hex strings, join
> them, add &H to it and then convert "&H3A98" to decimal.
>
> Maybe there is an easier way?
>
> Thanks!
>
Yes, there is. Above you go from a number over a string to a number. Ditch
the string and use the Integer bit manipulation functions:
Dim a, b As Integer
a = &H3a
b = &H98
Print a, b, Lsl(a, 8) Or b
Declaring a and b as Integer (not as Byte) is important here because you are
going to use the left shift function and you want to be sure that you don't
shift your bits to nirvana. Output is as expected:
58 152 15000
Also note that with numerical values there is no distinction between
"decimal" and "hex" or any other base you may imagine. A number is a number,
independent of a representation to a particular basis.
Regards,
Tobi
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list