How to make a flowgorithm that converts hexidecimal to decimal?

91 Views Asked by At

Hexidecimal to decimal Flowgorithm

Im confused about the functions and syntax. I need help in creating a flowgorithm that converts hexadecimal to decimal. I'd like to learn more about flowgorithm. I already tried making one and there are alot of errors like initializing and strings manipulation and alsoconverting string to integer. I am a beginner in this field.

1

There are 1 best solutions below

0
Costantino Grana On

I know that nobody will read this...

Let's assume that the question is "I have a String with the hexadecimal representation of a non negative number. How do I convert it into an Integer?"

First you need to be able to convert a single character into its value. We need to use the ASCII value of each char with ToCode(). Subtracting 48 you take numbers to their values. A nice way to manage uppercase and lowercase A to F is to take the reminder after dividing by 32 and then subtracting 7 gives the corresponding value:

HexCharVal

Now it's just a matter of applying the same function to every character of the string and multiplying by the correct power of 16:

HexStrVal

An example of using it:

Main

The XML version:

    <function name="Main" type="None" variable="">
        <parameters/>
        <body>
            <declare name="s" type="String" array="False" size=""/>
            <assign variable="s" expression="&quot;ff&quot;"/>
            <output expression="s &amp; &quot; (base 16) = &quot; &amp; HexStrVal(s) &amp; &quot; (base 10)&quot;" newline="True"/>
        </body>
    </function>
    <function name="HexCharVal" type="Integer" variable="val">
        <parameters>
            <parameter name="hexchar" type="String" array="False"/>
        </parameters>
        <body>
            <declare name="val" type="Integer" array="False" size=""/>
            <assign variable="val" expression="ToCode(hexchar) - 48"/>
            <if expression="val &gt; 10">
                <then>
                    <assign variable="val" expression="val % 32 - 7"/>
                </then>
                <else/>
            </if>
        </body>
    </function>
    <function name="HexStrVal" type="Integer" variable="val">
        <parameters>
            <parameter name="s" type="String" array="False"/>
        </parameters>
        <body>
            <declare name="i, val" type="Integer" array="False" size=""/>
            <assign variable="val" expression="0"/>
            <for variable="i" start="0" end="Len(s) - 1" direction="inc" step="1">
                <assign variable="val" expression="val * 16 + HexCharVal(Char(s,i))"/>
            </for>
        </body>
    </function>