PDA

View Full Version : Functions I hope will be of use.


Mike Halliday
04-20-2008, 09:32 AM
Here are 2 functions I've used in this months newsletter tutorial. Thought I would release them early.

I have tested them and they seem to work, but it is 20 years since I did any of this!! ha ha

func Hex2Dec(toConvert$)
Digits$ = "0123456789ABCDEF";
Base$ = 16;
Nbr$ = toConvert$;
NbrBase2$ = 0;
pwr$ = 0;
pwrrslt$ = 0;
ch$=0;
cl$ = 0;
atLen$ = Length(Nbr$);
For (chrLoop$, atLen$, 1, -1)
ch$ = INT(POS(MID(Nbr$, chrLoop$-1, 1), Digits$));
pwrrslt$ = POW(Base$, pwr$);
cl$ = (ch$ * pwrrslt$);
NbrBase2$ = (NbrBase2$ + cl$);
pwr$++;
end;
result$ = NbrBase2$;
return (result$);
end;

func Dec2Hex(toConvert$)
remaind$ = 0;
Digits$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Base$ = 16;
Nbr$ = toConvert$;
NbrBase2$ = "";
WHILE (NOT Nbr$ == 0)
remaind$ = RoundEX(Nbr$ MOD Base$,1);
NbrBase2$ = MID(Digits$, (Nbr$ MOD Base$) + 1, 1) % NbrBase2$;
Nbr$ = Nbr$ / Base$;
END ;
result$ = NbrBase2$;
return (result$);
end;

They are not clean or clevver, but you get the basic idea. Copy and paste them into your code and use as normal functions.

If you find problems with them, change them and re-post.

Enjoy

Mike.

~J~
04-20-2008, 12:51 PM
Crikey Mike, they are a blast from the past!!

Still, I reckon they could be of use somewhere. Such small but useful little functions when needed, be nice to have them built into the PPL language.

kornalius
04-20-2008, 03:19 PM
They are built in in the PPL language.

If you use a string like ShowMessage(int("0X0000FFFF")); PPL will convert it to decimal for you.

You can now use ShowMessage(printf("0x%08X", 1023)); to convert 1023 to a hexadecimal string.

kornalius
04-20-2008, 03:20 PM
But thanks to Mike, because that way you understand how it use to be done and how it really works.

Mike Halliday
04-20-2008, 04:13 PM
Thanks Alain, Is your way explained in the help? - I only wrote the functions because searching the help brought forth nothing! :S

kornalius
04-20-2008, 04:35 PM
The PPL language syntax section should explain the "0X0000F" part. PrintF() and SprintF() should be in the help file.

Mike Halliday
04-20-2008, 05:37 PM
Not being a C programmer myself I was unaware of the SPrintF or PrintF commands.


Typing HEX in the help brings up the example for HASH. If I type sprintf in the help there is a reference to Hex digits in the body of the help,

Does searching only look at the index of commands? It does look like it does not search the body of the help.

Does not really matter now as I have the routines for future use.

Thanks Alain for point out that PPL, yet again, has everything we need built in to it.

kornalius
04-21-2008, 01:12 AM
For the search feature in the help, I will have to let Eric answer that one.

PointOfLight
04-21-2008, 05:06 AM
If you're on the index tab it will only search topic names.* If you want to search the body text, you must go to the Search tab.* If I enter hex in the search tab, it pulls up the topic "Syntax".

Mike Halliday
04-21-2008, 06:56 AM
Found it. Thats great.

It only shows how to add a hex number to something else. How would I use this example to convert from Decimal to Hex then?

Can Printf be used to assign the resultant number to a variable? IE H$ = printf("0x%08X", 1023); Would H$ be equal to 1023HEX?

Mike Halliday
04-23-2008, 02:02 PM
Ok, so here's the thing. I tested my 2 funcs in a seperate program and they worked. Tested them in the newsletter tutorial and they returned incorrect hex and dec numbers.

Because of this I have re-written them using Alains suggestions and they work in all tests. So i have included them below.

// Convert Hexadecimal to Dec
func Hex2Dec(toConvert$)
result$ = int("0x"%toConvert$);
return (result$);
end;

// Convert Decimal to Hexadecimal
func Dec2Hex(toConvert$)
result$ = printf("%02X", toConvert$);
return (result$);
end;

:)