hex numbers

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

hex numbers

Bob Zawalich-3
This was a thread a while back. I think I had a quick look at what Neil
suggested for generalizing a base conversion and thought it would be too
much work. I might look into either adding Hex to Decimal only or else doing
something like what Martin did.

http://sibelius-manuscript-plug-in-developers.3224780.n2.nabble.com/Hex-Numb
ers-td7573038.html

_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev
Reply | Threaded
Open this post in threaded view
|

Re: hex numbers

Neil Sands

Yes, that's what I was half remembering. I was about to make the same suggestion again of piggy backing on the existing binary stuff!


On 22 Oct 2016 8:34 p.m., "Bob Zawalich" <[hidden email]> wrote:
This was a thread a while back. I think I had a quick look at what Neil
suggested for generalizing a base conversion and thought it would be too
much work. I might look into either adding Hex to Decimal only or else doing
something like what Martin did.

<a href="http://sibelius-manuscript-plug-in-developers.3224780.n2.nabble.com/Hex-Numb ers-td7573038.html" rel="noreferrer" target="_blank">http://sibelius-manuscript-plug-in-developers.3224780.n2.nabble.com/Hex-Numb
ers-td7573038.html

_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev

_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev
Reply | Threaded
Open this post in threaded view
|

Re: hex numbers

Bob Zawalich-3

I looked into doing what Martin did, but I decided to skip the binary stuff because it did a lot more processing that was really needed. Once you convert a hex digit to decimal, you just need to add it to 16 * the previous value, so why convert to binary and then back to decimal?

 

I wrote a HexToDec routine. My code handles negative numbers, upper or lower case a-f, and hex numbers that start with a “h” prefix, as used in SysEx MIDI messages.

 

I added a the routine to the attached plugin file. Its Run routine takes i = 0 to 1024, converts each number to Hex and then converts the Hex back to decimal.

 

 

Here is the code I came up with. You can find it in the attached plugin file with better formatting retained

 

HexToDec(numHex)

{

numConv = 0;

numOrig = numHex;

base = 16;

 

asc0 = Asc("0");

asc9 = Asc("9");

 

if (numHex = 0)

{

//trace("HexToDec hex, dec: " & numOrig & ", " & numConv);

return 0;

}

 

// strip off "h" prefix"

chFirst = Substring(numHex, 0, 1);

if (chFirst = "h")

{

numHex = Substring(numHex, 1);

}

 

// REVIEW could strip other prefixes, like "0x" here

 

fNeg = False;

if (numHex < 0)

{

fNeg = True;

numHex = -numHex;

}

 

numDigits = Length(numHex);

for i = 0 to numDigits

{

iDigit = i;

digitHex = Substring(numHex, iDigit, 1);

//trace("HTD digitHex, asc: " & digitHex & ", " & Asc(digitHex));

asc = Asc(digitHex); // get asc to avoid problems comparing string values

if ((asc >= asc0) and (asc <= asc9))

{

numDec = 0 + digitHex;

}

else

{

switch(digitHex)

{

case("A")

{

numDec = 10;

}

case("B")

{

numDec = 11;

}

case("C")

{

numDec = 12;

}

case("D")

{

numDec = 13;

}

case("E")

{

numDec = 14;

}

case("F")

{

numDec = 15;

}

case("a")

{

numDec = 10;

}

case("b")

{

numDec = 11;

}

case("c")

{

numDec = 12;

}

case("d")

{

numDec = 13;

}

case("e")

{

numDec = 14;

}

case("f")

{

numDec = 15;

}

default

{

Sibelius.MessageBox ("HexToDec invalid Hex character. Conversion failed. Returning 0. Invalid character = " & digitHex);

return 0;

}

}

}

 

numConv = (numConv * base) + numDec;

}

 

if (fNeg)

{

numConv = -numConv;

}

 

//trace("HexToDec hex, dec: " & numOrig & ", " & numConv);

return numConv;

}

 


_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev

ConvertDecimalHex.zip (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: hex numbers

Bob Zawalich-3
In reply to this post by Neil Sands

p.s.,

 

Consider any of the code here and in the plugin file to be public domain. Feel free to use if it you have any need of it.

 

From: Bob Zawalich [mailto:[hidden email]]
Sent: Saturday, October 22, 2016 2:54 PM
To: 'A mailing list for Sibelius plug-in developers' <[hidden email]>
Subject: RE: [Plugin-dev] hex numbers

 

I looked into doing what Martin did, but I decided to skip the binary stuff because it did a lot more processing that was really needed. Once you convert a hex digit to decimal, you just need to add it to 16 * the previous value, so why convert to binary and then back to decimal?

 

I wrote a HexToDec routine. My code handles negative numbers, upper or lower case a-f, and hex numbers that start with a “h” prefix, as used in SysEx MIDI messages.

 

I added a the routine to the attached plugin file. Its Run routine takes i = 0 to 1024, converts each number to Hex and then converts the Hex back to decimal.

 

Here is the code I came up with. You can find it in the attached plugin file with better formatting retained

 

HexToDec(numHex)

{

numConv = 0;

numOrig = numHex;

base = 16;

 

asc0 = Asc("0");

asc9 = Asc("9");

 

if (numHex = 0)

{

//trace("HexToDec hex, dec: " & numOrig & ", " & numConv);

return 0;

}

 

// strip off "h" prefix"

chFirst = Substring(numHex, 0, 1);

if (chFirst = "h")

{

numHex = Substring(numHex, 1);

}

 

// REVIEW could strip other prefixes, like "0x" here

 

fNeg = False;

if (numHex < 0)

{

fNeg = True;

numHex = -numHex;

}

 

numDigits = Length(numHex);

for i = 0 to numDigits

{

iDigit = i;

digitHex = Substring(numHex, iDigit, 1);

//trace("HTD digitHex, asc: " & digitHex & ", " & Asc(digitHex));

asc = Asc(digitHex); // get asc to avoid problems comparing string values

if ((asc >= asc0) and (asc <= asc9))

{

numDec = 0 + digitHex;

}

else

{

switch(digitHex)

{

case("A")

{

numDec = 10;

}

case("B")

{

numDec = 11;

}

case("C")

{

numDec = 12;

}

case("D")

{

numDec = 13;

}

case("E")

{

numDec = 14;

}

case("F")

{

numDec = 15;

}

case("a")

{

numDec = 10;

}

case("b")

{

numDec = 11;

}

case("c")

{

numDec = 12;

}

case("d")

{

numDec = 13;

}

case("e")

{

numDec = 14;

}

case("f")

{

numDec = 15;

}

default

{

Sibelius.MessageBox ("HexToDec invalid Hex character. Conversion failed. Returning 0. Invalid character = " & digitHex);

return 0;

}

}

}

 

numConv = (numConv * base) + numDec;

}

 

if (fNeg)

{

numConv = -numConv;

}

 

//trace("HexToDec hex, dec: " & numOrig & ", " & numConv);

return numConv;

}

 


_______________________________________________
Plugin-dev mailing list
[hidden email]
https://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev