MIDI-OX User Forum (http://www.midiox.com/cgi-bin/yabb/YaBB.pl)
MIDI-OX >> Scripting Questions >> Sysex HEX Conversion to Decimal
(Message started by: abhunkin on May 21st, 2007, 9:57pm)

Title: Sysex HEX Conversion to Decimal
Post by abhunkin on May 21st, 2007, 9:57pm
I need to convert the seventh byte of an incoming 8-byte sysex string to a decimal value in order to output it in a standard MIDI message. Here's the code:

Sub Sink_SysExInput(strSysEx)

   If Len(strSysEx) = 24 Then
     sysex = Split(strSysex)
     mox.OutputMidiMsg -1, 232, 0, sysex(6)
     Exit Sub
   End If

   mox.SendSysExString strSysEx

End Sub

The data conversion only works when the input byte (sysex(6)) < 10dec. The values will range from 0-127dec.

How can I convert this byte correctly?

TIA -

Title: Re: Sysex HEX Conversion to Decimal
Post by Jerry Jorgenrud on May 22nd, 2007, 8:12am
Replace sysex(6) with cint("&H" & sysex(6))

example script - save as something.vbs

strSysEx = "F0 42 13 56 45 7A 7F F7 "
If Len(strSysEx) = 24 Then
sysex = Split(strSysex)
msgbox sysex(6)
msgbox cint("&H" & sysex(6))
End if


Title: Re: Sysex HEX Conversion to Decimal
Post by abhunkin on May 22nd, 2007, 11:57am
Thanks, Jerry. That works perfectly.

In my case, the CInt cast seems optional, and my code runs well without it. Since I know the incoming value will be an integer between 0-127, is it safe to omit the CInt?

Title: Re: Sysex HEX Conversion to Decimal
Post by Jerry Jorgenrud on May 22nd, 2007, 3:03pm
As you know, VBScript uses only one kind of variable, known as a variant, which can store any kind of data and thus vbscript is called a "typeless" language. But under the hood the interpreter still must cast to types by "guessing" based on context. It didn't correctly guess your intent with sysex(6) until you added the &H as a hint that you wanted that substring interpreted as a hexadecimal number. Similarly, if I don't further cast that result to an int (in my example script) then it guesses wrong again and I get a string, not a number. If the input is "7F" instead of getting 127 I get "&H7F". In the context in which you're using it (i.e. not as an input to msgbox) VBSript is apparently guessing the way you want it to and giving you a number.

So is it safe to leave out the additonal cast? Depends on the context ... and one's personal risk aversion.

I personally prefer explicit casting because bugs come from ambiguity and the more you control the less can go wrong. Or at least you can't blame someone else when they do.  ;)



MIDI-OX User Forum » Powered by YaBB 1 Gold - SP 1.3.1!
YaBB © 2000-2003. All Rights Reserved.