Announcement

Collapse
No announcement yet.

miva_hex_encode function returns decimal value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    miva_hex_encode function returns decimal value

    miva_hex_encode function returns decimal value with '3' between digits

    Code:
    <mvt:eval expr="miva_hex_encode( 0 )" /><br>
    <mvt:eval expr="miva_hex_encode( 1 )" /><br>
    <mvt:eval expr="miva_hex_encode( 2 )" /><br>
    <mvt:eval expr="miva_hex_encode( 4 )" /><br>
    <mvt:eval expr="miva_hex_encode( 8 )" /><br>
    <mvt:eval expr="miva_hex_encode( 16 )" /><br>
    <mvt:eval expr="miva_hex_encode( 32 )" /><br>
    <mvt:eval expr="miva_hex_encode( 64 )" /><br>
    <mvt:eval expr="miva_hex_encode( 128 )" /><br>
    <mvt:eval expr="miva_hex_encode( 256 )" /><br>
    Returns:

    30
    31
    32
    34
    38
    3136
    3332
    3634
    313238
    323536

    I'm on MivaScript Engine v5.31. Does anyone have a work around?
    http://www.alphabetsigns.com/

    #2
    It's working but not in the way you're expecting. It is not converting the number to the hex equivalent, instead its converting each character to the hex ascii value of that character.

    <mvt:eval expr="miva_hex_encode( 256 )" /><br>

    I think what your were expecting was to convert the number to 'FF" but got 323536

    Compare to this chart. https://www.asciitable.com/

    2 = ascii character 33 hex
    5 = ascii character 35 hex
    6 = ascii character 36 hex

    resulting in 323536

    I'll update the description to better explain the distinction.

    For what it's worth I wish the function had been named asciivaluehex() instead. IMHO, there are way to many function names that start with "miva" that are not really miva system or array functions.


    I found this function in the LSK
    Code:
    <MvFUNCTION NAME = "DigitalDownload_Amazon_Date_ISO8601_Short" PARAMETERS = "time_t" STANDARDOUTPUTLEVEL = "">
        <MvFUNCTIONRETURN VALUE = "{ substring( DigitalDownload_Amazon_Date_ISO8601_Long( l.time_t ), 1, 8 ) }">
    </MvFUNCTION>
    
    <MvFUNCTION NAME = "DigitalDownload_Amazon_URIEncode" PARAMETERS = "input, encode_slash" STANDARDOUTPUTLEVEL = "">
        <MvASSIGN NAME = "l.output" VALUE = "">
    
        <MvFOR INDEX = "l.pos" COUNT = "{ len_var( l.input ) }">
            <MvASSIGN NAME = "l.char" VALUE = "{ substring_var( l.input, l.pos, 1 ) }">
    
            <MvIF EXPR = "{ NOT ISNULL l.char AND ( isalnum( l.char ) OR l.char EQ '_' OR l.char EQ '-' OR l.char EQ '~' OR l.char EQ '.' OR ( l.char EQ '/' AND NOT l.encode_slash ) ) }">
                <MvASSIGN NAME = "l.output" VALUE = "{ l.output $ l.char }">
            <MvELSE>
                <MvASSIGN NAME = "l.output" VALUE = "{ l.output $ '%' $ toupper( miva_hex_encode( l.char ) ) }">
            </MvIF>
        </MvFOR>
    
        <MvFUNCTIONRETURN VALUE = "{ l.output }">
    </MvFUNCTION>
    Last edited by RayYates; 04-14-18, 05:40 AM.
    Ray Yates
    "If I have seen further, it is by standing on the shoulders of giants."
    --- Sir Isaac Newton

    Comment


      #3
      i'm still trying to get over the fact we have to type mv in front of everything...
      Bruce Golub
      Phosphor Media - "Your Success is our Business"

      Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
      phosphormedia.com

      Comment


        #4
        Originally posted by Bruce - PhosphorMedia View Post
        i'm still trying to get over the fact we have to type mv in front of everything...
        I guess you could write a "pre-compiler" and change the syntax. So then you could compile to MivaScript so it can compile to assembly and finally compile to Empressa VM <grin />
        Ray Yates
        "If I have seen further, it is by standing on the shoulders of giants."
        --- Sir Isaac Newton

        Comment


          #5
          This function will convert a decimal number to a hexadecimal string.

          Code:
          <MvFUNCTION NAME="dec2hex" PARAMETERS="num" STANDARDOUTPUTLEVEL="">
              <MvWHILE EXPR="{ l.num NE 0 }">
                  <MvASSIGN NAME="l.temp" VALUE="{ l.num mod 16 }">
                  <MvIF EXPR="{ l.temp LT 10 }">
                      <MvASSIGN NAME="l.ndx" VALUE="{ miva_array_insert( l.array, l.temp + 48, -1 ) }">
                  <MvELSE>
                      <MvASSIGN NAME="l.ndx" VALUE="{ miva_array_insert( l.array, l.temp + 55, -1 ) }">
                  </MvIF>
          
                  <MvASSIGN NAME="l.num" VALUE="{ (l.num - l.temp ) / 16 }">
              </MvWHILE>
          
              <MvASSIGN NAME="l.count" VALUE="{ miva_array_elements(l.array) }">
              <MvFOR FIRST = "{ 1 }" LAST = "{ l.count }">
                  <MvASSIGN NAME="l.return" VALUE="{ l.return $ asciichar(miva_array_pop( l.array ))  }">
              </MvFOR>
          
              <MvFUNCTIONRETURN VALUE="{ l.return }">
          </MvFUNCTION>
          Examples:
          dec2hex(256) returns 100
          dec2hex(65535) returns FFFF
          dec2hex(120999) returns 1D8A7

          NOTE: Converting certain very large numbers seem to expose a bug where the MvWhile loop fails to exit even l.num = 0
          4000000001 is one of those numbers. I have not determined the cause of the apparent bug. This could be happening when the math results in a number larger than the VM can handle.
          Last edited by RayYates; 04-16-18, 05:55 AM. Reason: Caveat added
          Ray Yates
          "If I have seen further, it is by standing on the shoulders of giants."
          --- Sir Isaac Newton

          Comment


            #6
            Here is the companion function.
            Code:
            <MvFUNCTION NAME="hex2dec" PARAMETERS="num" STANDARDOUTPUTLEVEL="">
                <MvASSIGN NAME="l.num" VALUE="{ toupper(trim(l.num)) }">
                <MvASSIGN NAME="l.len" VALUE="{ len(l.num) }">
                <MvIF EXPR="{ l.len LT 1}">
                    <MvFUNCTIONRETURN>
                </MvIF>
            
                <MvASSIGN NAME="l.base" VALUE="{ 1 }">
                <MvFOR INDEX = "l.pos" FIRST = "{ 1 }" LAST = "{ l.len }">
                    <MvASSIGN NAME="l.char_value" VALUE="{ asciivalue(substring(l.num, l.len - l.pos + 1, 1)) }">
            
                    <MvIF EXPR="{ (l.char_value GE 48) AND (l.char_value LE 57) }">
                        <MvASSIGN NAME="l.dec_val" VALUE="{ l.dec_val + ((l.char_value - 48) * l.base) }">
            
                    <MvELSEIF EXPR="{ (l.char_value GE 65) AND (l.char_value LE 70) }">
                        <MvASSIGN NAME="l.dec_val" VALUE="{ l.dec_val + ((l.char_value - 55) * l.base) }">
            
                    <MvELSE>
                        <MvASSIGN NAME="g.hex2dec_error" VALUE="Error: Invalid hex characters found.">
                        <MvFUNCTIONRETURN>
                    </MvIF>
            
                    <MvASSIGN NAME="l.base" VALUE="{ l.base * 16 }">
                </MvFOR>
            
                <MvFUNCTIONRETURN VALUE="{ l.dec_val }">
            </MvFUNCTION>
            Ray Yates
            "If I have seen further, it is by standing on the shoulders of giants."
            --- Sir Isaac Newton

            Comment


              #7
              Hi Ray,

              Thank you for that explanation. I was digging around some very old threads so figured there had to be a workaround. The functions are a great example of using the ascii table that I hadn't though of..

              Code:
              <mvt:assign name="g.data" value="256" />
              
              <mvt:eval expr="miva_variable_type( g.data )" />
              
              // RETURNS INTEGER
              
              <mvt:eval expr="miva_variable_type( miva_hex_encode( g.data ) )" />
              
              // RETURNS STRING
              So I guess in javascript everything is an object and in Miva everything is a string.

              Thanks a million.




              http://www.alphabetsigns.com/

              Comment


                #8
                Glad to help Dan.
                Ray Yates
                "If I have seen further, it is by standing on the shoulders of giants."
                --- Sir Isaac Newton

                Comment

                Working...
                X