16 bit value in hex with MSB and LSB?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

16 bit value in hex with MSB and LSB?

alexchernoff
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!

Reply | Threaded
Open this post in threaded view
|

Re: 16 bit value in hex with MSB and LSB?

Tobias Boege-2
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: 16 bit value in hex with MSB and LSB?

zxMarce
In reply to this post by alexchernoff
Alex,

What you're receiving is two characters with ASCII values 3A (":") and
98 (not printable, ASCII code over 7F). In any case, there's no need to
convert anything to hex.
If bytes come in LSB first, you can use the *Integer@* function along
with the *StrPtr* (or similar, don't remember in Gambas) function and
get your 16-bit value back directly.
If they come MSB first and your system is LSB first, then you can swap
them and apply byte maths:

*  Dim firstByte As Byte = Asc(Left(myString, 1)**)                'This
gets the &H3A ASCII value from character ":" in firstByte.
**  Dim nextByte As Byte = Asc(Mid(myString, 2, 1))              
**'This gets the &h98 ASCII value in nextByte.**
**  Dim wordValue As Integer = firstByte * &H100 + nextByte       'This
gets &H3A98 in wordValue*

I guess some byte juggling can be performed by the underlying Stream
object in the serial port (to accomodate "endianness"), but never used
it, so I cannot tell.

Hope that helps,
zxMarce.

*On 18/05/17 06:28, 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!

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/gambas-user
Reply | Threaded
Open this post in threaded view
|

Re: 16 bit value in hex with MSB and LSB?

alexchernoff
In reply to this post by Tobias Boege-2
awesome!

Lsl() does not have a definition (F2) in Gambas UI it seems though, at least not 3.9.2.

also too bad Gambas UI help does not have a Search function (or does it?)

thanks!