` Hex to decimal conversion (Eric Churton ) - Icetips Article
Icetips - Templates, Tools & Utilities for Clarion Developers

Templates, Tools and Utilities
for Clarion Developers

Icetips Article

Back to article list   Search Articles     Add Comment     Printer friendly     Direct link  

Par2: Hex to decimal conversion
1999-01-31 -- Eric Churton
 
This question has often come up. If one understands what one is trying to do, then it becomes possible to work out one or more solutions. Hopefully the following observations will help somebody. Bear in mind that there are lots of different (and probably better) ways of doing these conversions (usually involving arrays), but I have worked these methods out so that they are reasonable easy to understand, improve on and duplicate. In computer programming skinning a cat is often done in a hurry, so one tends to use the first method one thinks of! 1. Converting Decimal to Hex Lets compare decimal with hexadecimal. Decimal numbers are represented by 10 possible characters: 0123456789, anytime 9 is exceeded you bump up a 1 on the left and make '9' '0' - thus 9 + 1 becomes 10 and 99 + 1 becomes 100. Hexadecimal numbers are represented by 16 possible characters 0123456789ABCDEF, in this instance when F is exceeded you bump up 1 on the left and turn the F into 0. A represents decimal 10, B represents decimal 11 etc - thus F + 1 becomes 10 and FF + 1 becomes 100 (every sixteenth character rather than every 10th turns the odometer over). Remember that the actual *value* of the number is the same whatever form it is displayed in. In other words, what you are trying to do is to take a number and represent it as a string in different formats - the same way you can take a LONG and display it as different date formats. If you have a string to place the result into you could work on the following basis: keep looping until your number -1 = 0, your number being the one you want to convert Increment the rightmost character by one i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F, testing to see if 'F' has been reached. If it has been reached set it back to 0 and increment the next character on the left by 1, first testing to see if 'F' has been reached. If it has been reached, set it to 0 you move a further character to the left, repeating the process until you reach the start of the string or the current character is less than 'F' This is a simple counter, and if you display it between increments it will show a speedometer type counter. Once you realise how this works, you can short-circuit it by using division and modulus. By having a string of 16 characters e.g. Hexchars = '0123456789ABCDEF' you can select the value by using string slicing Hexchars STRING(16) hexdisplaystring STRING(30) ! Can handle up to 30 characters - use a different length if you need to stringposition SHORT ! will point to the character you are handling number LONG ! holds the value you want to convert - prime this before using... tempnumber LONG ! use this for working so number is not affected CODE Hexchars = '0123456789ABCDEF' clear(hexdisplaystring) stringposition = len(hexdisplaystring) tempnumber = number LOOP WHILE (tempnumber AND stringposition) ! if you run out of number or space this will end the loop hexdisplaystring[stringposition] = hexchars[tempnumber%16 + 1] ! fetch hex char tempnumber = tempnumber/16 ! the rightmost char is finished with so adjust the value - dividing a long loses the remainder (or modulus, depending on how old you are) stringposition -= 1 ! next char !note: either stringposition will become 0 or tempnumber will become 0 ! you can test for overflow here if you want to e.g ! if not stringposition AND tempnumber - i.e. you still have a value to evaluate but no space !Message ('The number is too large for the string') ! hexdisplaystring = 'Overflow' ! or '' or whatever you want to do END Here it is without comments, (and tested....) ! NOTE: you can create the hexchars variable in the Data of the procedure, setting initial value to 0123456789ABCDEF clear(hexdisplaystring) stringposition = len(hexdisplaystring) tempnumber = number LOOP WHILE (tempnumber AND stringposition) hexdisplaystring[stringposition] = hexchars[tempnumber%16 + 1] tempnumber = tempnumber/16 stringposition -= 1 ! next char if stringposition = 0 and tempnumber message('Overflow') end END 2. Hex to decimal This is much easier because it just consists of adding up the values of each character of the hex string. Thus: hexstring string(30) number (long) ! where you will store the number stringposition LONG counter LONG tempnumber LONG ! you would first prime the hexstring with a hexadecimal string CODE number = 0 ! start with nothing stringposition = len(clip(hexstring)) tempnumber = 0 counter = 0 LOOP WHILE (stringposition ) ! stop when you have evaluated the whole string if not counter ! i.e. the first time through counter = 1 else counter = counter * 16 ! 16 256 4096 65536 etc end CASE hexdisplaystring[stringposition] OF '0' to '9' number += counter * hexdisplaystring[stringposition] ELSE number += counter * (10 + ( VAL(hexdisplaystring[stringposition]) - VAL('A') ) ) ! this puts 'A' to 10, 'B' to 11 and so on - make sure that the string entered is UPPER or amend this code to change to UPPER END tempnumber += 1 stringposition -= 1 END This also has been tested. Most of the time solving this sort of problem is very difficult if you don't know what you are trying to accomplish. I think Russell Egan has often said (something like) when you have a problem and there is any part of it you don't understand, go back a step until you understand that part. Arnor Baldvinson notes that there is a sample app shipping with Clarion showing how to do hex-decimal conversions.


Today is April 25, 2024, 11:32 am
This article has been viewed 35115 times.



Back to article list   Search Articles   Add Comment   Printer friendly

Login

User Name:

Password: