max Posted April 15, 2018 Report Share Posted April 15, 2018 I'm attempting to create a 2-character random HEX value. I've done a 'Variable Set Integer' using random numbers from 1 to 9, and a 'Variable Set String' from random letters but have the following issues: I am unable to limit the random letters to 'A' through 'F' (for HEX nomenclature) The final value will always be a number and letter, or whatever order it is set to. Any suggestions? Quote Link to comment Share on other sites More sharing options...
rberq Posted April 15, 2018 Report Share Posted April 15, 2018 A hex value expressed by two characters can be anything from x'00' to x'FF', which is decimal 0 to 255. So you could (1) Generate a random integer from 0 to 255. (2) Using Macro Express's integer arithmetic, divide the generated integer by 16, ignoring the remainder, and you will have a quotient from 0 to 15 representing the leftmost hex character. (3) The remainder from the above division will likewise be an integer from 0 to 15 repesenting the rightmost hex character. (4) Use a simple lookup array, or a series of IF statements, to convert the two integers to letters/numbers. IF QUOTIENT = 0 THEN LEFTLETTER = '0' IF QUOTIENT = 1 THEN LEFTLETTER = '1' ... ... IF QUOTIENT = 14 THEN LEFTLETTER = 'E' IF QUOTIENT = 15 THEN LEFTLETTER = 'F' ... ... IF REMAINDER = 0 THEN RIGHTLETTER = '0' IF REMAINDER = 1 THEN RIGHTLETTER = '1' etc. There is probably a more elegant way to do it, but I'm into brute force this morning. Quote Link to comment Share on other sites More sharing options...
max Posted April 15, 2018 Author Report Share Posted April 15, 2018 That is brutal (in a positive way)! Thank you very much. Bear with me, but in using integer arithmetic wouldn't I only be getting a whole number an no remainders? ...which is fine actually as it just means I need to 'double the recipe' to get the double characters that I want. And in terms of converting the integers, I think I only need to do that for generating 'A' through 'F'. Having said that, I am having trouble with the IF statements in the conversion. Would I use a "Variable Set to ASCII Char"? Quote Link to comment Share on other sites More sharing options...
rberq Posted April 15, 2018 Report Share Posted April 15, 2018 Yes, integer arithmetic would give a whole number quotient. To isolate the remainder you could multiply the quotient by 16 and subtract the product from the original random number -- again entirely with integer arithmetic. Or as you say, just generate two separate random numbers, one for each character you want to end up with. I tried "Variable Set to ASCII Char" and it did not product a useful result. I was assuming that you want displayable characters -- for example, random number 26 is hexadecimal x'1A', so you want two displayable characters, '1' and 'A'. You are right about having to work hard only for generating characters 'A' through 'F'. The test macro below will properly produce a text string for value '9' but will NOT produce an 'E' for value 14. Run it and see. Variable Set Integer %N1% to 9 Variable Modify Integer: Convert %N1% to text string %T1% Variable Set Integer %N2% to 14 Variable Modify Integer: Convert %N2% to text string %T2% Text Box Display: %T1% %T2% But you don't know ahead of time whether you will be generating a numeric character or a letter character. The series of IF statements I suggested will handle both cases. Quote Link to comment Share on other sites More sharing options...
max Posted April 15, 2018 Author Report Share Posted April 15, 2018 Ok, great. Could you give me some assistance converting the number to text? The closest I could find is using the following: Variable Modify String: Replace %N1% in %T10% with "A" Quote Link to comment Share on other sites More sharing options...
rberq Posted April 15, 2018 Report Share Posted April 15, 2018 Sixteen IF statements in all, to place a displayable text character into a text variable: If Variable %N1% = 0 Variable Set String %T10% "0" End If If Variable %N1% = 1 Variable Set String %T10% "1" End If .... If Variable %N1% = 10 Variable Set String %T10% "A" End If .... If Variable %N1% = 15 Variable Set String %T10% "F" End If 1 Quote Link to comment Share on other sites More sharing options...
max Posted April 16, 2018 Author Report Share Posted April 16, 2018 Gottit! Thank you for your assistance and patience. Quote Link to comment Share on other sites More sharing options...
terrypin Posted May 8, 2018 Report Share Posted May 8, 2018 Hadn't seen this until today, but if you're still around you might try this alternative for fun. Works OK here on first couple of tests. Web Site, "https://www.browserling.com/tools/random-hex", using Default Web Browser Delay: 1 seconds // Using a Repeat allows easier trial/error via the included Delay. Repeat Start (Repeat 11 times) // Tab to 'How many digits?' Text Type (Simulate Keystrokes): <TAB> Delay: 0.1 seconds End Repeat Delay: 1 seconds // Trial/error! Text Type (Simulate Keystrokes): 2 // Specify two digits. Delay: 0.2 seconds Text Type (Simulate Keystrokes): <TAB> // Tab to 'How many results?' Delay: 0.1 seconds Text Type (Simulate Keystrokes): 1 // Only a single result wanted Delay: 0.1 seconds Text Type (Simulate Keystrokes): <TAB> // Tab to 'Generate' Delay: 0.1 seconds Text Type (Simulate Keystrokes): <ENTER> // Apply Delay: 0.1 seconds Text Type (Simulate Keystrokes): <SHIFT><TAB><SHIFT><TAB><SHIFT><TAB> // Tab back to the line under the result Delay: 0.1 seconds Text Type (Simulate Keystrokes): <BACKSPACE><SHIFT><HOME> // Select the result Delay: 0.1 seconds Clipboard Copy Delay: 0.2 seconds Variable Set String %tResult% from the clipboard contents Variable Modify String: Replace "%tResult%" in %tResult% with "0x%tResult%" Text Box Display: Result <WEB SITE URL="https://www.browserling.com/tools/random-hex" Wait="FALSE" Default_Browser="TRUE" NoEmbeddedVars="FALSE" _IGNORE="0x000C"/> <DELAY Flags="\x01" Time="1"/> <COMMENT Value="Using a Repeat allows easier trial/error via the included Delay."/> <REPEAT START Start="1" Step="1" Count="11" Save="FALSE" _COMMENT="Tab to 'How many digits?'"/> <TEXT TYPE Action="0" Text="<TAB>"/> <DELAY Flags="\x01" Time="0.1"/> <END REPEAT/> <DELAY Flags="\x01" Time="1" _COMMENT="Trial/error!"/> <TEXT TYPE Action="0" Text="2" _COMMENT="Specify two digits."/> <DELAY Flags="\x01" Time="0.2"/> <TEXT TYPE Action="0" Text="<TAB>" _COMMENT="Tab to 'How many results?'"/> <DELAY Flags="\x01" Time="0.1"/> <TEXT TYPE Action="0" Text="1" _COMMENT="Only a single result wanted"/> <DELAY Flags="\x01" Time="0.1"/> <TEXT TYPE Action="0" Text="<TAB>" _COMMENT="Tab to 'Generate'"/> <DELAY Flags="\x01" Time="0.1"/> <TEXT TYPE Action="0" Text="<ENTER>" _COMMENT="Apply"/> <DELAY Flags="\x01" Time="0.1"/> <TEXT TYPE Action="0" Text="<SHIFT><TAB><SHIFT><TAB><SHIFT><TAB>" _COMMENT="Tab back to the line under the result"/> <DELAY Flags="\x01" Time="0.1"/> <TEXT TYPE Action="0" Text="<BACKSPACE><SHIFT><HOME>" _COMMENT="Select the result"/> <DELAY Flags="\x01" Time="0.1"/> <CLIPBOARD COPY/> <DELAY Flags="\x01" Time="0.2"/> <VARIABLE SET STRING Option="\x02" Destination="%tResult%" NoEmbeddedVars="FALSE"/> <VARIABLE MODIFY STRING Option="\x0F" Destination="%tResult%" ToReplace="%tResult%" ReplaceWith="0x%tResult%" All="FALSE" IgnoreCase="FALSE" NoEmbeddedVars="FALSE"/> <TEXT BOX DISPLAY Title="Result" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 tResult = %tResult%\r\n\\par \\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/> RandomHex-ViaWeb.mex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.