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

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to