Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Hexadecimal Conversion Functions

    Functions
    6
    8
    1146
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pianofire
      pianofire Fuze Team last edited by pianofire

      Description: Functions to convert from decimal to hexadecimal and vice versa
      Share Code : NXKGHEZDNN

      // Hexadecimal Conversion Functions
      
      // function :   dec2hex
      // description: convert an integer to a hexadecimal strkng
      // author :     @pianofire
      // arguments :
      //     number : decimal number
      // returns :
      //     hexadecimal string
      function dec2hex( number )
          var result = ""
          while number > 0 loop
              result = chr( number % 16 + 48 + int( number % 16 > 9 ) * 7 ) + result  
              number = int( number / 16 )
          repeat
          if len( result ) % 2 == 1 then
              result = "0" + result
          endif
          if len(result) == 0 then
              result = "00"
          endif
      return result 
      
      // function :   hex2dec
      // description: convert a hexadecimal string t0 a decimal number
      // author :     @pianofire
      // arguments :
      //     hexString : hexadecimal string
      // returns :
      //     decimal number
      function hex2dec( hexString )
          var result = 0
          var val = 0
          hexString = toUpper( hexString )
          for i = 0 to len( hexString ) loop
              val = chrVal( hexString[ i ] )
              if val > 65 then
                result = result * 16 + val - chrVal( "A" ) + 10
              else
                result = result * 16 + val - chrVal( "0" )
              endif  
          repeat
      return result
      
      // Convert a string to upper case 
      function toUpper( stringVar )
          var result = ""
          for i = 0 to len( stringVar ) loop
              val = chrVal( stringVar[i] )
              if val >= 97 and val <= 122 then
                  val -= 32
              endif
              result += chr( val )
          repeat
      return result
      
      // Convert a string to lower case
      function toLower( stringVar )
          var result = ""
          for i = 0 to len( stringVar ) loop
              val = chrVal( stringVar[i] )
              if val >= 65 and val <= 90 then
                  val += 32
              endif
              result += chr( val )
          repeat
      return result
      
      // Unit test code by @PB____
      
      // print with new line
      function printLn( value )
      	print( value, "\n" )
      return void
      
      // join a series of values together in a string
      function join(values)
          string result = ""
          int i
          for i = 0 to len( values ) loop
      	result += str( values[ i ] )
          repeat
      return result
      
      // assert that value1 is equal to value2
      function assertEqual( value1, value2 )
          string str1 = str( value1 )
          string str2 = str( value2 )
          string outcome = ""
          if str1 == str2 then
      	ink( lime )
      	outcome = "success"
          else
      	ink( red )
      	outcome = "fail"
          endif
          println( join( [ "assert ", str1, " equals to ", str2, ": ", outcome ] ) )
      return void
      
      // print test description
      function description( text )
          ink( { 0.5, 0.5, 0.5, 1 } )
          println(text)
      return void
      
      // run unit tests with the specified delay at the end
      function runUnitTests( duration )
          var inputNumber = 590
          var outputNumber = 0
      
          description( "test dec2hex" ) 
          hexadecimalString = dec2hex( inputNumber )
          assertEqual( hexadecimalString, "024E" )
      
          description( "test hex2dec" )   
          outputNumber = hex2dec( hexadecimalString )
          assertEqual( outputNumber, inputNumber )
      
          description( "test lower case" )   
          outputNumber = hex2dec( toLower(hexadecimalString) )
          assertEqual( outputNumber, inputNumber )
      
          description( "test upper case" )   
          outputNumber = hex2dec( toUpper(hexadecimalString) )
          assertEqual( outputNumber, inputNumber )
      
          description( "test dec2hex( 0 )" ) 
          hexadecimalString = dec2hex( 0 )
          assertEqual( hexadecimalString, "00" )
      
          update()
          sleep( duration )
      return void
      
      runUnitTests( 10 )
      
      1 Reply Last reply Reply Quote 5
      • pianofire
        pianofire Fuze Team last edited by

        Updated to fix 0 case (thanks @Martin)

        1 Reply Last reply Reply Quote 1
        • vinicity
          vinicity F last edited by

          I can’t seem to be able to download this? Has it been taken down?

          1 Reply Last reply Reply Quote 0
          • pianofire
            pianofire Fuze Team last edited by pianofire

            Sorry a lot of my stuff was accidentally unshared. I have resubmitted it

            N1MCZGNDNN

            1 Reply Last reply Reply Quote 3
            • AlienDjinn
              AlienDjinn last edited by

              Just curious, is the plan to make these built-in functions to an upcoming version of Fuze? Thanks for make these happen @pianofire

              1 Reply Last reply Reply Quote 2
              • Dave
                Dave Fuze Team last edited by

                @AlienDjinn it is not something currently on our to-do list, but if the need for it is there then it is certainly something we'll consider!

                1 Reply Last reply Reply Quote 1
                • S
                  Slydog last edited by

                  It would be a useful feature to have an 'Insert Code' option in the editor which would list useful community code snippets to paste into your code.
                  The code in the list would have to be approved by an admin of course. (Maybe a 'Submit Code' option from the editor?)
                  And an entry may include many functions if they are required to make a larger system. (ie. an inventory system which includes AddItem, DeleteItem, etc)
                  To seed it, have a contest to see who can submit the best snippet!

                  eg: "Insert Code -> Math -> Conversion -> DecToHex()"

                  1 Reply Last reply Reply Quote 2
                  • PickleCatStars
                    PickleCatStars F last edited by

                    That’s a great idea!

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post