How to make an app - Andromo App Maker

Announcement

Collapse
No announcement yet.

Convert Hexadecimal to Decimal

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

  • Convert Hexadecimal to Decimal

    Ted Sullivan has previously posted a nice LUA script for converting decimal number to hex:

    Code:
    function Dec2Hex(nValue)
    	if type(nValue) == "string" then
    		nValue = String.ToNumber(nValue);
    	end
    	nHexVal = string.format("%X", nValue);  -- %X returns uppercase hex, %x gives lowercase letters
    	sHexVal = nHexVal.."";
    	return sHexVal;
    end
    I wonder, is it possible to use the LUA string library to do the opposite conversion? I couldn't find the answer by searching the Net.

    I needed a solution and consequently I wrote my own script. In case any of you should need it:

    Code:
    function Hex2Dec(sValue)
    	bFigErr = false;
    	if type(sValue) == "number" then
    		sValue = sValue.."";
    	end
    	sValue = String.TrimLeft(sValue, "0");
    	nDigits = String.Length(sValue);
    	nTotal = 0;
    -- 414 = 4*16^2 + 1*16^1 + 4*16^0  power = base^exp
    	while nDigits > 0 do
    		sIn = String.Upper(String.Left(sValue, 1));
    		if sIn > "9" then
    			if sIn == "A" then
    				nFactor = 10;
    			elseif sIn == "B" then
    				nFactor = 11;
    			elseif sIn == "C" then
    				nFactor = 12;
    			elseif sIn == "D" then
    				nFactor = 13;
    			elseif sIn == "E" then
    				nFactor = 14;
    			elseif sIn == "F" then
    				nFactor = 15;
    			else
    				-- error
    				bFigErr = true;
    				sTotal = "";
    				break;
    			end
    		else
    			nFactor = String.ToNumber(sIn);
    		end
    		-- go
    		nTotal = nTotal + nFactor * Math.Pow(16, nDigits - 1);
    		error = Application.GetLastError();
    		if error == 0 then
    			sValue = String.TrimLeft(sValue, sIn);
    			nDigits = nDigits - 1;
    		else
    			sTotal = "";
    			bFigErr = true;
    			break;
    		end
    	end
    	if not bFigErr then
    		sTotal = nTotal.."";
    	end
    	return sTotal;
    end
    Why I needed it? I was about to test the language code in MS Office (Hi, SUF6NEWBIE). A Registry call returns decimal values like 1033 (English). I put the MS codes into a table and could use this action

    Code:
    sOfficeLanguage = sOfficeLanguageCode.." - "..Language(sOfficeLanguageCode);
    to report: Language 1033 – English.

    BUT, the Windows language codes are hexadecimal values (and I had built my table with decimal values only. But now:

    Code:
    sOSLangCodeHex = Registry.GetValue(HKEY_LOCAL_MACHINE, sRegKey, "InstallLanguage", true);
    sOSLanguageCode = Hex2Dec(sOSLangCodeHex);
    sOSlanguage = sOSLanguageCode.." - "..Language(sOSLanguageCode);
    and I get: Language: 1044 – Norwegian (from 0414h)

  • #2
    I believe the function tonumber will convert HEX to decimal if you specify a base 10 like this
    Code:
    decimalval = tonumber(FF, 10);
    tigg
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

    Comment


    • #3
      This is from Lua.org documentation

      Originally posted by Lua.org
      tonumber (e [, base])
      Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns that number; otherwise, it returns nil.
      An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter `A´ (in either upper or lower case) represents 10, `B´ represents 11, and so forth, with `Z´ representing 35. In base 10 (the default), the number may have a decimal part, as well as an optional exponent part (see 2.2.1). In other bases, only unsigned integers are accepted.
      TJ-Tigger
      "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
      "Draco dormiens nunquam titillandus."
      Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

      Comment


      • #4
        Oops, the wheel is an old invention?

        My problem: I'm not able to utilize the information in the Lua.org documentation to write workable code in AMS50.

        414h --> 1044 ??? How?

        Comment


        • #5
          Originally posted by csd214
          Oops, the wheel is an old invention?

          My problem: I'm not able to utilize the information in the Lua.org documentation to write workable code in AMS50.

          414h --> 1044 ??? How?
          Here try this.

          decimalvalue = tonumber("414", 16);

          It needs to be in base 16 for hex. Sorry about that.

          Tigg
          TJ-Tigger
          "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
          "Draco dormiens nunquam titillandus."
          Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

          Comment


          • #6
            Ah, I "found the wheel".

            T H A N K S, Tigg. :yes

            Nearly more than 100% decrement of code lines.

            Code:
            function Hex2Dec(sValue)
            -- nDecimalValue = tonumber(sValue, 16);	
            -- the Lua function returns a variable of type 'number'
            -- we want this function to return a 'string' variable
            	sDecimalValue = tonumber(sValue, 16).."";				
            	return sDecimalValue;
            	
            end	-- function Hex2Dec(sValue) ********************

            Comment


            • #7
              You could remove one more line if you are so inclined

              Code:
              function Hex2Dec(sValue)
              -- nDecimalValue = tonumber(sValue, 16);	
              -- the Lua function returns a variable of type 'number'
              -- we want this function to return a 'string' variable
              	return tonumber(sValue, 16).."";				
              end	-- function Hex2Dec(sValue) ********************
              TJ-Tigger
              "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
              "Draco dormiens nunquam titillandus."
              Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

              Comment


              • #8
                Yes, of course. Thank you.

                I try to "avoid" the LuaCom plug-in if it's possible. Some times; as in this case; LuaCOM is great, but unfortunately I have problems with understanding the manual. It reminds me of legal stuff (agreements) written by an US attorney. English isn't my native language…

                Another case were LuaCOM is imperative is the sort order (e.g. in a list box) when you use an international character set (outside A-Z).

                The advantage of my HEX struggling: I had to refresh my knowledge of the hex system. Now I'm able to convert hex to decimal with mental arithmetic. YOU helped me with more efficient coding. I hope this thread is useful to many folks out there.

                Comment


                • #9
                  Me too. Always happy to help.

                  Tigg
                  TJ-Tigger
                  "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
                  "Draco dormiens nunquam titillandus."
                  Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

                  Comment


                  • #10
                    Hey could you guys have a look at this one..for original Hex string to ASCII
                    can the code be streamlined etc..tks(TJ and CSD tks for the help)

                    --functions to convert a true HEX string to ASCII char

                    Code:
                    function Hex2Ascii()
                    	--converts the Hex string to Char and appends to any existing from loop
                    	strAscii_work = String.Concat(strAscii_work, string.format("%c", tonumber(strhxmid, 16)..""));
                    end
                    
                    
                    function HEXCYCLE()
                    	strAscii_work = "";
                    	local hxlength = String.ToNumber(String.Length(strHex));		
                    	while (hxlength > 0) do
                    		strhxmid = String.Mid(strHex, 1, 2);
                    		if (strhxmid ~= "") then --(covers getting stuck in loop if an error)
                    		
                    			Hex2Ascii(strhxmid); --call ASCII converter
                    			
                    			strHex = String.Mid(strHex, 3, -1); --remove converted from string for next
                    			local hxlength = String.ToNumber(String.Length(strHex)); --for loop
                    			if (hxlength == 0) then
                    			break; --all done
                    			end
                    		end												
                    	end--while
                    	
                    return strAscii_work; --converted string	
                    		
                    end
                    --EOHEXtoAscii
                    
                    --to call it try below
                    
                    --eg: find the drive letter for one of your cdrom devices and change below to suit(eg J: to yours)
                    strHex = Registry.GetValue(HKEY_LOCAL_MACHINE, "SYSTEM\\MountedDevices", "\\DosDevices\\J:", true);
                    strASCII = HEXCYCLE(strHex); --call func to cycle through and call converter
                    Dialog.Message("Debug info: converted Hex to ASCII", strASCII);
                    Last edited by SUF6NEWBIE; 02-04-2005, 01:00 PM.

                    Comment


                    • #11
                      Decimal to Hexadecimal

                      Code:
                      function Dec2Hex(nValue)
                      	if type(nValue) == "string" then
                      		nValue = String.ToNumber(nValue);
                      	end
                      	nHexVal = string.format("%X", nValue);  -- %X returns uppercase hex, %x gives lowercase letters
                      	sHexVal = nHexVal.."";
                      	return sHexVal;
                      end
                      how would I implement this code, to grab decimal numbers from an INPUT box, and convert them to hexadecimal with the click of a button? Thanks

                      Comment


                      • #12
                        You would call that code in "on click".

                        Comment


                        • #13
                          I know that lol

                          I know that lol but where would I put:

                          Code:
                          dec = Input.GetText("Input1");
                          
                          Input.SetText("Input1", "");

                          Comment

                          Working...
                          X