[Gambas-user] 16 bit value in hex with MSB and LSB?
ML
d4t4full at ...626...
Thu May 18 12:00:54 CEST 2017
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!
More information about the User
mailing list