Hex Numbers

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

Hex Numbers

Martin Marris
I need to enter some hexadecimal numbers into a ManuScript array. No problem
is they are converted to decimal (and possibly back again later) but how do
I do that? There doesn't seem to be a "utils" routine for that
("CastDecimalToHex" and "CastHexTo Decimal" or whatever).

It would only take a few lines to write my own converter, but was just
wondering if someone has been there before....

The hex number series to convert and put into an array are very simple,
something like this:
<0001>
<0002>
<0003>
<0004>
<0005>
<0006>
<0007>
<0008>
<0009>
<000A>
<000B>
<000C>
<000D>
<0007>
<000E>

Martin


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

Re: Hex Numbers

Bob Zawalich
I have never needed to do that. There are none in utils, so I suspect you
are on your own, unless someone on the list other than me has some already.

If it were me, I would store them as strings, with a 0x prefix, and only
convert as needed. If you don't have conversion routines around you can
google some that would be easy enough to put into MS.

Good luck!

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Martin Marris
Sent: Monday, September 28, 2015 6:31 PM
To: 'A mailing list for Sibelius plug-in developers'
Subject: [Plugin-dev] Hex Numbers

I need to enter some hexadecimal numbers into a ManuScript array. No problem
is they are converted to decimal (and possibly back again later) but how do
I do that? There doesn't seem to be a "utils" routine for that
("CastDecimalToHex" and "CastHexTo Decimal" or whatever).

It would only take a few lines to write my own converter, but was just
wondering if someone has been there before....

The hex number series to convert and put into an array are very simple,
something like this:
<0001>
<0002>
<0003>
<0004>
<0005>
<0006>
<0007>
<0008>
<0009>
<000A>
<000B>
<000C>
<000D>
<0007>
<000E>

Martin


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

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

Re: Hex Numbers

Martin Marris
Thanks Bob. You are up late ... and I am up early.

Remind me: how do I make arrays persistent between Methods, i.e. pass an
array from one Method to another?

Do I need to use Dictionaries for that? Or can I plunk the array into a Data
field?

(I have asked you all of these questions before, but I am very rusty.)

Tnx.

Martin


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

Re: Hex Numbers

Martin Marris
In reply to this post by Bob Zawalich
Going back to the subject of hex numbers, yes, I had thought of just
handling them as strings. This will entail many cycles of string-matching
routines to match an entry in the array with an entry in a text file ...
there are thousands of items to parse. If it's too slow, I'll see if I can
find/write a way to convert the hex strings to decimal numbers.

Martin


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

Re: Hex Numbers

Neil Sands
It might (or might not) be less work to take the utils binary stuff as
a starting point and convert from there to hex. Each 4 digits of
binary corresponds to 1 digit in hex.
_______________________________________________
Plugin-dev mailing list
[hidden email]
http://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev
Reply | Threaded
Open this post in threaded view
|

Re: Hex Numbers

Martin Marris
>> It might (or might not) be less work to take the utils binary stuff as a
starting point and convert from there to hex. Each 4 digits of binary
corresponds to 1 digit in hex.<<

True, that's a fairly easy piece of programming. I've been reading up about
direct hex => decimal conversion and it's slightly head-reeling, with a lot
of "remainder" arithmetic.

Thank you for the tip!

What I'm doing is taking taking already existent arrays from text file data.
The original array indices are all hexadecimal when the array is built, and
when the array elements are later referenced, they are also referenced using
the hex index numbers.

So I'm thinking that it makes sense to go straight to a decimal number
rather than messing around with strings, both for greater speed and just to
make it all easier to understand.

My only other thought was that maybe ManuScript supports hex numbers
natively, but when I tried doing some simple hex arithmetic (adding two hex
numbers) it threw up an error, so I can forget that avenue....

Martin


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

Re: Hex Numbers

Bob Zawalich-3
Martin -
Have you started writing that code? If not why not let me write a pair of conversion routines? We can all code review them and I can offer to put them into utils.plg for a future version of Sib.

Though feel free to write them yourself if you prefer...

Bob (from his iPhone)

On Sep 29, 2015, at 2:52 AM, Martin Marris <[hidden email]> wrote:

>>> It might (or might not) be less work to take the utils binary stuff as a
> starting point and convert from there to hex. Each 4 digits of binary
> corresponds to 1 digit in hex.<<
>
> True, that's a fairly easy piece of programming. I've been reading up about
> direct hex => decimal conversion and it's slightly head-reeling, with a lot
> of "remainder" arithmetic.
>
> Thank you for the tip!
>
> What I'm doing is taking taking already existent arrays from text file data.
> The original array indices are all hexadecimal when the array is built, and
> when the array elements are later referenced, they are also referenced using
> the hex index numbers.
>
> So I'm thinking that it makes sense to go straight to a decimal number
> rather than messing around with strings, both for greater speed and just to
> make it all easier to understand.
>
> My only other thought was that maybe ManuScript supports hex numbers
> natively, but when I tried doing some simple hex arithmetic (adding two hex
> numbers) it threw up an error, so I can forget that avenue....
>
> Martin
>
>
> _______________________________________________
> Plugin-dev mailing list
> [hidden email]
> http://avid-listsrv1.avid.com/mailman/listinfo/plugin-dev

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

Re: Hex Numbers

Martin Marris

Hi Bob,

 

This is what I wrote and it’s working fine:

 

hex_binary = "";

for i = 0 to 4

     {

     hex_digit = Substring(hex_input, i, 1);

     switch (hex_digit)              //parse each hex digit and convert to 4-digit binary

           {

           case("0") {binary = "0000";}

           case("1") {binary = "0001";}

           case("2") {binary = "0010";}

           case("3") {binary = "0011";}

           case("4") {binary = "0100";}

           case("5") {binary = "0101";}

           case("6") {binary = "0110";}

           case("7") {binary = "0111";}

           case("8") {binary = "1000";}

           case("9") {binary = "1001";}

           case("A") {binary = "1010";}

           case("B") {binary = "1011";}

           case("C") {binary = "1100";}

           case("D") {binary = "1101";}

           case("E") {binary = "1110";}

           case("F") {binary = "1111";}

           }

     hex_binary = hex_binary & binary;//concatenate 4-digit binaries to a 16-digit number

     }

decimal = utils.DenaryValue(hex_binary);//convert binary to decimal

 

return decimal;

 

This only works for four-digit hex numbers, but then, all of the hex digits I'm parsing are four-digit.

 

It would certainly be worth having a more generalized “utils” that would convert hex to binary, and hex to decimal, regardless of digit length.

 

Best,

 

Martin

 


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

Re: Hex Numbers

Neil Sands
Love it. No arithmetic involved :)

On 29 September 2015 at 16:56, Martin Marris <[hidden email]> wrote:
> Hi Bob,
>
>
>
> This is what I wrote and it’s working fine:
>

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

Re: Hex Numbers

Bob Zawalich
Yeah, this looks fine, and if it is doing what you need there is probably no need to do much else. At my leisure I will see  if I can come up with a matching reverse calculation, though in 15 years of doing this I have never needed such a calculation.

DenaryValue does a fair bit of arithmetic, of course, but this code piggybacks on it nicely, and Substring is an efficient way to  lop off the digits.

Nicely done,

Bob

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Neil Sands
Sent: Tuesday, September 29, 2015 9:00 AM
To: A mailing list for Sibelius plug-in developers
Subject: Re: [Plugin-dev] Hex Numbers

Love it. No arithmetic involved :)

On 29 September 2015 at 16:56, Martin Marris <[hidden email]> wrote:
> Hi Bob,
>
>
>
> This is what I wrote and it’s working fine:
>

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


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

Re: Hex Numbers

Bob Zawalich
In reply to this post by Martin Marris

Here is a general purpose routine to convert any decimal number to hex format (digits 0 – f only)

 

It will round the input to deal with floating point, handles negative numbers, and has an option to have A-F or a-f.

 

The attached plugin runs it from -50 to 50 in the run routine.

 

The code below is copied from a text file, so has the quotes at start and end

 

Cheers

 

bob

 

            DecimalToHex "(valDecIn, fUseLowerCase) {

val = Round(valDecIn); // ensure positive integer.

if (val = 0) 

{

            return '0';

}

 

fNeg = False;

if (val > 0)

{

            valNeg = False;

}

else

{

            fNeg = True;

            val = -val;

}

 

if (fUseLowerCase)

{

            ascii_a_Start = Asc('a') - 10;  // offset so 10 yields 'a'

}

else

{

            ascii_a_Start = Asc('A') - 10;  // offset so 10 yields 'A'

}

 

ascii_zero = Asc('0');

str = '';

 

while (val != 0)

{

            digit = val % 16; // remainder after dividing by 16

            if (digit < 10)

            {

                        digit = digit + ascii_zero; // converting number to string    

            }

            else

            {

                        digit = digit + ascii_a_Start;        

            }

 

            str = Chr(digit) & str; // concat in reverse order

 

            val = val / 16;  // this will be integer division

}

 

if (fNeg)

{

            str = '-' & str;

}

 

return str;

}"

 

-50: -32

-49: -31

-48: -30

-47: -2f

-46: -2e

-45: -2d

-44: -2c

-43: -2b

-42: -2a

-41: -29

-40: -28

-39: -27

-38: -26

-37: -25

-36: -24

-35: -23

-34: -22

-33: -21

-32: -20

-31: -1f

-30: -1e

-29: -1d

-28: -1c

-27: -1b

-26: -1a

-25: -19

-24: -18

-23: -17

-22: -16

-21: -15

-20: -14

-19: -13

-18: -12

-17: -11

-16: -10

-15: -f

-14: -e

-13: -d

-12: -c

-11: -b

-10: -a

-9: -9

-8: -8

-7: -7

-6: -6

-5: -5

-4: -4

-3: -3

-2: -2

-1: -1

0: 0

1: 1

2: 2

3: 3

4: 4

5: 5

6: 6

7: 7

8: 8

9: 9

10: a

11: b

12: c

13: d

14: e

15: f

16: 10

17: 11

18: 12

19: 13

20: 14

21: 15

22: 16

23: 17

24: 18

25: 19

26: 1a

27: 1b

28: 1c

29: 1d

30: 1e

31: 1f

32: 20

33: 21

34: 22

35: 23

36: 24

37: 25

38: 26

39: 27

40: 28

41: 29

42: 2a

43: 2b

44: 2c

45: 2d

46: 2e

47: 2f

48: 30

49: 31


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

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

Re: Hex Numbers

Martin Marris

Thanks very much Bob – let’s hope it will make its way into the shipping “utils” eventually.

 

Martin

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Bob Zawalich
Sent: Tuesday, September 29, 2015 20:36
To: 'A mailing list for Sibelius plug-in developers'
Subject: Re: [Plugin-dev] Hex Numbers

 

Here is a general purpose routine to convert any decimal number to hex format (digits 0 – f only)

 

It will round the input to deal with floating point, handles negative numbers, and has an option to have A-F or a-f.

 

The attached plugin runs it from -50 to 50 in the run routine.

 

The code below is copied from a text file, so has the quotes at start and end

 

Cheers

 

bob

 

            DecimalToHex "(valDecIn, fUseLowerCase) {

val = Round(valDecIn); // ensure positive integer.

if (val = 0) 

{

            return '0';

}

 

fNeg = False;

if (val > 0)

{

            valNeg = False;

}

else

{

            fNeg = True;

            val = -val;

}

 

if (fUseLowerCase)

{

            ascii_a_Start = Asc('a') - 10;  // offset so 10 yields 'a'

}

else

{

            ascii_a_Start = Asc('A') - 10;  // offset so 10 yields 'A'

}

 

ascii_zero = Asc('0');

str = '';

 

while (val != 0)

{

            digit = val % 16; // remainder after dividing by 16

            if (digit < 10)

            {

                        digit = digit + ascii_zero; // converting number to string    

            }

            else

            {

                        digit = digit + ascii_a_Start;        

            }

 

            str = Chr(digit) & str; // concat in reverse order

 

            val = val / 16;  // this will be integer division

}

 

if (fNeg)

{

            str = '-' & str;

}

 

return str;

}"

 

-50: -32

-49: -31

-48: -30

-47: -2f

-46: -2e

-45: -2d

-44: -2c

-43: -2b

-42: -2a

-41: -29

-40: -28

-39: -27

-38: -26

-37: -25

-36: -24

-35: -23

-34: -22

-33: -21

-32: -20

-31: -1f

-30: -1e

-29: -1d

-28: -1c

-27: -1b

-26: -1a

-25: -19

-24: -18

-23: -17

-22: -16

-21: -15

-20: -14

-19: -13

-18: -12

-17: -11

-16: -10

-15: -f

-14: -e

-13: -d

-12: -c

-11: -b

-10: -a

-9: -9

-8: -8

-7: -7

-6: -6

-5: -5

-4: -4

-3: -3

-2: -2

-1: -1

0: 0

1: 1

2: 2

3: 3

4: 4

5: 5

6: 6

7: 7

8: 8

9: 9

10: a

11: b

12: c

13: d

14: e

15: f

16: 10

17: 11

18: 12

19: 13

20: 14

21: 15

22: 16

23: 17

24: 18

25: 19

26: 1a

27: 1b

28: 1c

29: 1d

30: 1e

31: 1f

32: 20

33: 21

34: 22

35: 23

36: 24

37: 25

38: 26

39: 27

40: 28

41: 29

42: 2a

43: 2b

44: 2c

45: 2d

46: 2e

47: 2f

48: 30

49: 31


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

Re: Hex Numbers

Neil Sands
In reply to this post by Bob Zawalich

It should be quite easy I think to adapt this for any base until you run out of alphabet at base 36. You could pass two arguments to the method, X and Y, and it could return X in base Y.

On 30 Sep 2015 01:36, "Bob Zawalich" <[hidden email]> wrote:

Here is a general purpose routine to convert any decimal number to hex format (digits 0 – f only)

 

It will round the input to deal with floating point, handles negative numbers, and has an option to have A-F or a-f.

 

The attached plugin runs it from -50 to 50 in the run routine.

 

The code below is copied from a text file, so has the quotes at start and end

 

Cheers

 

bob

 

            DecimalToHex "(valDecIn, fUseLowerCase) {

val = Round(valDecIn); // ensure positive integer.

if (val = 0) 

{

            return '0';

}

 

fNeg = False;

if (val > 0)

{

            valNeg = False;

}

else

{

            fNeg = True;

            val = -val;

}

 

if (fUseLowerCase)

{

            ascii_a_Start = Asc('a') - 10;  // offset so 10 yields 'a'

}

else

{

            ascii_a_Start = Asc('A') - 10;  // offset so 10 yields 'A'

}

 

ascii_zero = Asc('0');

str = '';

 

while (val != 0)

{

            digit = val % 16; // remainder after dividing by 16

            if (digit < 10)

            {

                        digit = digit + ascii_zero; // converting number to string    

            }

            else

            {

                        digit = digit + ascii_a_Start;        

            }

 

            str = Chr(digit) & str; // concat in reverse order

 

            val = val / 16;  // this will be integer division

}

 

if (fNeg)

{

            str = '-' & str;

}

 

return str;

}"

 

-50: -32

-49: -31

-48: -30

-47: -2f

-46: -2e

-45: -2d

-44: -2c

-43: -2b

-42: -2a

-41: -29

-40: -28

-39: -27

-38: -26

-37: -25

-36: -24

-35: -23

-34: -22

-33: -21

-32: -20

-31: -1f

-30: -1e

-29: -1d

-28: -1c

-27: -1b

-26: -1a

-25: -19

-24: -18

-23: -17

-22: -16

-21: -15

-20: -14

-19: -13

-18: -12

-17: -11

-16: -10

-15: -f

-14: -e

-13: -d

-12: -c

-11: -b

-10: -a

-9: -9

-8: -8

-7: -7

-6: -6

-5: -5

-4: -4

-3: -3

-2: -2

-1: -1

0: 0

1: 1

2: 2

3: 3

4: 4

5: 5

6: 6

7: 7

8: 8

9: 9

10: a

11: b

12: c

13: d

14: e

15: f

16: 10

17: 11

18: 12

19: 13

20: 14

21: 15

22: 16

23: 17

24: 18

25: 19

26: 1a

27: 1b

28: 1c

29: 1d

30: 1e

31: 1f

32: 20

33: 21

34: 22

35: 23

36: 24

37: 25

38: 26

39: 27

40: 28

41: 29

42: 2a

43: 2b

44: 2c

45: 2d

46: 2e

47: 2f

48: 30

49: 31


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


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