Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939

    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. #2
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  3. #3
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    This is from Lua.org documentation

    Quote 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

  4. #4
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    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?

  5. #5
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    Quote 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

  6. #6
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    Ah, I "found the wheel".

    T H A N K S, Tigg.

    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) ********************

  7. #7
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  8. #8
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    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.

  9. #9
    Join Date
    Sep 2002
    Location
    Sol 3
    Posts
    3,160
    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

  10. #10
    SUF6NEWBIE Guest
    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 at 12:00 PM.

  11. #11
    Join Date
    Oct 2009
    Posts
    18

    Smile 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

  12. #12
    Join Date
    Jan 2001
    Location
    Anderson Island, WA, USA
    Posts
    2,861
    You would call that code in "on click".


    (Click here to contact me)
    Providing Independent Professional Consulting Services for
    IndigoRose products, World Wide.
    Located in -8:00 (-7:00 DST) GMT Timezone (Western United States)

  13. #13
    Join Date
    Oct 2009
    Posts
    18

    I know that lol

    I know that lol but where would I put:

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

Similar Threads

  1. Is there any way to change the page color by code?
    By itamar in forum AutoPlay Media Studio 5.0
    Replies: 9
    Last Post: 11-20-2005, 12:21 AM
  2. How to convert XML database to SQLite?
    By dmla in forum AutoPlay Media Studio 5.0
    Replies: 2
    Last Post: 10-20-2004, 01:28 AM
  3. Quick Tip: Bytes to kb -mb -gb
    By SUF6NEWBIE in forum AutoPlay Media Studio 5.0
    Replies: 12
    Last Post: 08-31-2004, 01:07 PM
  4. Program: Binary Hex and Decimal Converter
    By Protocol in forum AutoPlay Media Studio 5.0 Examples
    Replies: 3
    Last Post: 07-15-2004, 12:11 PM
  5. Converting hex value to decimal...can we do this in SF6??
    By Martin_SBT in forum Setup Factory 6.0
    Replies: 5
    Last Post: 02-06-2003, 02:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts