|
Post by djsorgan on Aug 15, 2023 1:45:53 GMT
Is there an easy way to convert a hex byte (00 to 7F) to a decimal value (0 to 127) in Stream Byter? I am controlling the tempo of the Dynamic Clock. It requires a Sysex with decimal values (F0 5A I0 I1 I2 F7). My remote input is in Hex. How can I convert the input hex to a decimal value?
A formula that works is Decimal Value = (MSB * 128) + LSBd. I know how to convert this to the required Dynamic Clock Tempo input.
For example, if the input is F0 58 08 01 7E F7 (tempo = 254), the input to the Dynamic Clock is F0 5A 02 54 00 F7. The issue is converting the 5th byte (7E) to a decimal for the LSB in the above formula. Is there an easy formula or must a 128 element array be created to use as a lookup? Can the array be implemented using elements represented in hex with data being the decimal. For example ... L7D ==> 125, L7E ==> 126, L7F ==> 127.
For the above example, the formula is: Decimal Value = (1 * 128) + 126 = 254, where LSB 7Eh ==> 126d
|
|
|
Post by redheronmusic on Aug 16, 2023 15:32:22 GMT
This should be posted in the StreamByter section as more appropriate to your question.
For ease of testing, this code is configured to use the SB interface to enter numbers and see the pseudo BCD conversion. For production use, grab the code from BEGIN to END.
# Red Heron Music # Aug 16, 2023 # Covert 2 x 7 byte hex values (eg MIDI Bytes) to pseudo BCD values # max valid input value is 9999 or 4E 0F (have to add third output byte to represent higher inputs)
# configured to use Q0, Q1 as inputs, results shown in LB0, LB1 # Input controls are in hex (displayed as decimal) # output is hex
# pseudo BCD logic is fairly simple # divide & mod by 100 to split MSB / LSB # if digit value is between # 11 and 19, you have to add six to the hex value # 20 - 29, add 12 # etc
# for production use, you want the code from BEGIN to END
# i0 is total input value # i1 is base 10 MSB # i2 is base 10 LSB # i3 is MSB pseudo BCD # i4 is LSB pseudo BCD
If load Set Q0 MSB Set Q1 LSB Set LB0 0 Set LB1 0 Ass Q0 = 0 0 End
IF M0 == F0 7D 01 # BEGIN Mat i0 = Q0 * $128 Mat i0 = i0 + Q1 ‘ i0 now is total value Mat i1 = i0 / $100 ‘ get MSB Mat i2 = i0 % $100 ‘ remainder
# Convert MSB to pseudo BCD Mat i3 = i1 / $10 Mat i3 = i3 * 6 Mat i1 = i1 + i3
# Covert LSB to pseudo BCD Mat i4 = i2 / $10 Mat i4 = i4 * 6 Mat i2 = i2 + i4 # END
Set LB0 i1 Set LB1 i2 End
|
|