Finding little-endian values of 24-bit numbers

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
mikefromca
Coder
Posts: 12
Joined: Wed Oct 16, 2019 11:28 am

Finding little-endian values of 24-bit numbers

Post by mikefromca » Wed Oct 16, 2019 11:51 am

I'm doing a project in which data is sent from the PC to my own custom hardware via a serial port.

I coded my own hardware so that any numeric parameters needs to be in little-endian format. But I'm finding the PC likes to deal with numbers in big endian format.

Nice thing with Qbasic (which seems to be the only programming language I know that supports this directly) is that I can use mki$ and cvi and friends to help me in my quest.

So as an example, say the required packet format my hardware expects is as follows:

Byte 1: Function number
Bytes 2-4: 24-bit data
Byte 5: checksum

So my qbasic program will convert the string-hex format of the data to ascii to send down the serial line. By string-hex I mean where it prints hex characters as string.

For example, If I wanted to use function number 10 and the 24-bit data is the 24 bit number 254, and the checksum is 20, then in my program, I would have it process the string:

"0AEF000014"

Because 0A is hex for 10, EF0000 is little-endian 24-bit value for 254 and 14 is hex for 20.

Eventually I want to make these values not fixed. Say I want the user to input the 24-bit value in and he types 254 as the number

Somehow I want it to go down the serial line as data equivalent to qbasic's chr$(&HFE)+chr$(0)+chr$(0)

I'll write code to illustrate my point:

Code: Select all

funcn%=10
checksum%=14
myvalue&=254
'convert to output for serial
toser$=chr$(funcn%)+chr$(myvalue& AND 255)+chr$((myvalue&/256) AND &H100)+chr$((myvalue&/&H10000) AND 255)+chr$(checksum%)
;convert to user readable output
bytes$=""
for n%=1 to len(toser$)
bytes$=bytes$+right$("00"+hex$(asc(mid$(toser$,n%,1))),2)
next n%
print bytes$
The above code would achieve what I want but in reality the data packet size is much larger and may involve more numbers.

If my number was a 32-bit number, I could maybe replace my thought of:

Code: Select all

... chr$(myvalue& AND 255)+chr$((myvalue&/256) AND &H100)+chr$((myvalue&/&H10000) AND 255)+chr$(chr$((myvalue&/&H1000000))
with this:

Code: Select all

mkl$(myvalue&)
But what would be best for a 24-bit number? I'm just trying to cut down on the use of chr$() and number divisions.

User avatar
burger2227
Veteran
Posts: 2378
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: Finding little-endian values of 24-bit numbers

Post by burger2227 » Wed Oct 16, 2019 12:38 pm

Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

mikefromca
Coder
Posts: 12
Joined: Wed Oct 16, 2019 11:28 am

Re: Finding little-endian values of 24-bit numbers

Post by mikefromca » Thu Oct 17, 2019 11:16 pm

I was thinking more endian on byte-level not bit level.

For example,

Converting &H0000FE to &HFE0000 programmatically with simple commands, not multiple for-next loops going through each ascii character.

Perhaps I should learn more 8086 assembly and use call absolute more.

Post Reply