Re: [Gambas-user] 16 bit value in hex with MSB and LSB?
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! -- View this message in context: http://gambas.8142.n7.nabble.com/16-bit-value-in-hex-with-MSB-and-LSB-tp59029p59032.html Sent from the gambas-user mailing list archive at Nabble.com. -- 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
Re: [Gambas-user] 16 bit value in hex with MSB and LSB?
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 ASCII value from character ":" in firstByte. ** Dim nextByte As Byte = Asc(Mid(myString, 2, 1)) **'This gets the ASCII value in nextByte.** ** Dim wordValue As Integer = firstByte * + nextByte 'This gets 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 to it and then convert "" 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 Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
Re: [Gambas-user] 16 bit value in hex with MSB and LSB?
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 to it and then convert "" 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 = b = 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
[Gambas-user] 16 bit value in hex with MSB and LSB?
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 to it and then convert "" to decimal. Maybe there is an easier way? Thanks! -- View this message in context: http://gambas.8142.n7.nabble.com/16-bit-value-in-hex-with-MSB-and-LSB-tp59029.html Sent from the gambas-user mailing list archive at Nabble.com. -- 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