|
Post by ptitjoz on Aug 8, 2018 10:11:13 GMT 1
Hello With Libreoffice Calc, there is a function that allows you to encrypt by rotating 13 example =ROT13 (" Basic") will give Onfvpwould it be possible to imitate it in Basic Converter? Thank you Regards
|
|
|
Post by vovchik on Aug 8, 2018 11:33:01 GMT 1
Dear ptitjoz,
I did this thing a long time ago and just compiled and tested again. It works:
' ***************************************************** ' PROGRAM: rot13.bac ' PURPOSE: rot13 enconder/decoder ' AUTHOR: vovchik (Puppy Linux forum) ' DEPENDS: gcc, bacon ' PLATFORM: Puppy Linux (actually, any *nix) ' DATE: 16-02-2012 ' NOTES: based on rosetta code: ' http://en.wikipedia.org/wiki/ROT13 ' LICENSE: GPL3 ' *****************************************************
' ********************* ' SUBS & FUNCTIONS ' *********************
' ------------------ FUNCTION ROT13$(STRING input_str$) ' ------------------ LOCAL i TYPE NUMBER LOCAL ret_str$, tmp_str$ TYPE STRING IF EQUAL(input_str$, "") THEN ' return nothing if input string empty RETURN input_str$ ELSE ' otherwise examine string FOR i = 1 TO LEN(input_str$) tmp_str$ = MID$(input_str$, i, 1) IF ASC(tmp_str$) < ASC("A") \ OR (ASC(tmp_str$) > ASC("Z") \ AND ASC(tmp_str$) < ASC("a")) \ OR ASC(tmp_str$) > ASC("z") THEN ' take punctuation and space chars "as is" and concat ret_str$ = CONCAT$(ret_str$, tmp_str$) ELSE ' otherwise code/decode real letters and concat ' use binary "and", i.e. "&" IF (int)(ASC(tmp_str$) & DEC("DF")) < ASC("N") THEN ret_str$ = CONCAT$(ret_str$, CHR$(ASC(tmp_str$) + 13)) ELSE ret_str$ = CONCAT$(ret_str$, CHR$(ASC(tmp_str$) - 13)) END IF END IF NEXT i RETURN ret_str$ END IF END FUNCTION
' ********************* ' END SUBS & FUNCTIONS ' *********************
' ********************* ' MAIN ' *********************
REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE END
' ********************* ' END MAIN ' *********************
And Peter did a much more compact version here:
REM REM Demo implementing ROT13 encoder/decoder REM REM Theory here: http://en.wikipedia.org/wiki/ROT13 REM REM June 2009 - PvE. REM Revised November 2009. REM
REM Get the separate arguments SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
IF dim < 2 THEN PRINT "Usage: rot13 <string>" END ENDIF
FOR i = 1 TO LEN(arg$[1])
ch = ASC(MID$(arg$[1], i, 1))
IF ch > 64 AND ch < 78 THEN PRINT CHR$(ch + 13); ELIF ch > 77 AND ch < 91 THEN PRINT CHR$(ch - 13); ELIF ch > 96 AND ch < 110 THEN PRINT CHR$(ch + 13); ELIF ch > 109 AND ch < 123 THEN PRINT CHR$(ch - 13); ELSE PRINT CHR$(ch); END IF
NEXT PRINT
Here is Peter's version that I just recast as a function. It works nicely and is compact:
' ********************* ' SUBS & FUNCTIONS ' *********************
' ------------------ FUNCTION ROT13$(STRING input_str$) ' ------------------ LOCAL i, ch TYPE int LOCAL ret_str$ TYPE STRING FOR i = 1 TO LEN(input_str$) ch = ASC(MID$(input_str$, i, 1)) IF ch > 64 AND ch < 78 THEN ret_str$ = ret_str$ & CHR$(ch + 13) ELIF ch > 77 AND ch < 91 THEN ret_str$ = ret_str$ & CHR$(ch - 13) ELIF ch > 96 AND ch < 110 THEN ret_str$ = ret_str$ & CHR$(ch + 13) ELIF ch > 109 AND ch < 123 THEN ret_str$ = ret_str$ & CHR$(ch - 13) ELSE ret_str$ = ret_str$ & CHR$(ch) END IF NEXT i RETURN ret_str$ END FUNCTION
' ********************* ' END SUBS & FUNCTIONS ' *********************
' ********************* ' MAIN ' *********************
REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE END
' ********************* ' END MAIN ' *********************
And here is another variation:
DEF FN RANGE(v0, v1, v2)=IIF(v0 >= MIN(v1, v2) AND v0 <= MAX(v1, v2), 1, 0)
' ------------------ FUNCTION ROT13$(STRING input_str$) ' ------------------ LOCAL i, ch TYPE int LOCAL ret_str$ TYPE STRING FOR i = 1 TO LEN(input_str$) ch = ASC(MID$(input_str$, i, 1)) IF RANGE(ch, 65,77) THEN ret_str$ = ret_str$ & CHR$(ch + 13) ELIF RANGE(ch, 78, 90) THEN ret_str$ = ret_str$ & CHR$(ch - 13) ELIF RANGE(ch, 97, 109) THEN ret_str$ = ret_str$ & CHR$(ch + 13) ELIF RANGE(ch, 110, 122) THEN ret_str$ = ret_str$ & CHR$(ch - 13) ELSE ret_str$ = ret_str$ & CHR$(ch) END IF NEXT i RETURN ret_str$ END FUNCTION
' ********************* ' END SUBS & FUNCTIONS ' *********************
' ********************* ' MAIN ' *********************
REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE END
And here is still another that I ported from one AUTOIT implementation:
' ------------------ FUNCTION ROT13$(STRING input_str$) ' ------------------ LOCAL i, ch, o TYPE int LOCAL ret_str$, ch$ TYPE STRING FOR i = 1 TO LEN(input_str$) ch = ASC(MID$(input_str$, i, 1)) o = ASC("A") * (ASC("A") <= ch && ch <= ASC("Z")) \ + ASC("a") * (ASC("a") <= ch && ch <= ASC("z")) IF o > 0 THEN ch = MOD(ch - o + 13, 26) ch$ = CHR$(ch + o) ELSE ch$ = CHR$(ch) END IF ret_str$ = ret_str$ & ch$ NEXT i RETURN ret_str$ END FUNCTION
' ********************* ' END SUBS & FUNCTIONS ' *********************
' ********************* ' MAIN ' *********************
REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE
With kind regards, vovchik
|
|
|
Post by bigbass on Aug 9, 2018 14:13:52 GMT 1
Hello vovchik Just to be complete here is another official snippet Converting strings is something Interesting and the system tr comnand is very short The bacon tr conversion is This from the rosseta code site rosettacode.org/wiki/Rot-13PRINT "Output: ", REPLACE$(s$, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM", 2)
|
|
|
Post by vovchik on Aug 9, 2018 14:34:08 GMT 1
Dear Joe, Thanks for that. It is terse. here it is as a tiny function: DEF FN ROT13$(x)=REPLACE$(x, \ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", \ "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM", 2)
PRINT ROT13$("Lbh penpxrq gur pbqr!") REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE END And here is another method using REGEX: ' ------------------ FUNCTION ROT13$(STRING s$) ' ------------------ LOCAL i, ch TYPE int LOCAL ch$, o$ TYPE STRING FOR i = 1 TO LEN(s$) ch = ASC(MID$(s$, i, 1)) ch$ = CHR$(ch) IF REGEX(ch$, "[A-Ma-m]") THEN INCR ch, 13 ELIF REGEX(ch$, "[N-Zn-z]") THEN DECR ch, 13 END IF o$ = o$ & CHR$(ch) NEXT i RETURN o$ END FUNCTION
PRINT ROT13$("Lbh penpxrq gur pbqr!") REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE With kind regards, vovchik
|
|
|
Post by bigbass on Aug 9, 2018 18:17:35 GMT 1
Hello vovchik I didn't write the bacon example on the rosetta code page above the author will be in the log file there will check later I didn't realize the power of the replace with multiple translations it can work like the tr command I like how you used the REGX (and the macro too) I was going to port this below but you covered the regx part of the idea already stackoverflow.com/questions/5442436/using-rot13-and-tr-command-for-having-an-encrypted-email-address echo 'fooman@example.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
different tools for different jobs but what is most important is seeing the formula what needs to be done then get creative Joe
|
|
|
Post by vovchik on Aug 9, 2018 18:46:20 GMT 1
Dear Joe, I didn't realize that REPLACE$ could also function as tr. What a great example. With kind regards, vovchik
|
|
|
Post by ptitjoz on Aug 10, 2018 9:20:55 GMT 1
Hello everyone Thank you for all your answers and codes very powerful. There are really good codes. The use of Replace is powerful. I tried to code a program in my own way but it is not very efficient. Maybe use arrays? Best Regards
REM Test Rot13 V01 by PtitJoz 10/08/2018 a$="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" b$="nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
input$="fooman@example.com" output$=""
FOR i%=1 TO LEN(input$) flag%=0 FOR j%=1 TO LEN(a$) IF MID$(input$,i%,1)=MID$(a$,j%,1) THEN output$=output$&MID$(b$,j%,1) flag%=1 BREAK END IF NEXT IF flag%=0 THEN output$=output$&MID$(input$,i%,1) NEXT PRINT output$
|
|
|
Post by vovchik on Aug 10, 2018 10:20:53 GMT 1
Dear ptitjoz, This is one way of using a char array for input: ' ------------------ FUNCTION ROT13$(char* in_str) ' ------------------ LOCAL i, ch TYPE int LOCAL o$ TYPE STRING FOR i = 0 TO LEN(in_str) - 1 ch = in_str[i] IF REGEX(CHR$(ch), "[A-Ma-m]") THEN INCR ch, 13 ELIF REGEX(CHR$(ch), "[N-Zn-z]") THEN DECR ch, 13 END IF o$ = o$ & CHR$(ch) NEXT i RETURN o$ END FUNCTION
inp$="fooman@example.com" PRINT ROT13$(inp$) PRINT ROT13$("Lbh penpxrq gur pbqr!") REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT ROT13$(my_str$) UNTIL FALSE As you can see, there are many options/solutions, and the most concise is Joe's REPLACE$ version, probably followed by either of my REGEX versions. They all work. With kind regards, vovchik
|
|
|
Post by bigbass on Aug 10, 2018 14:25:56 GMT 1
|
|
|
Post by vovchik on Aug 11, 2018 10:56:10 GMT 1
Dear Joe, There is a lot of useful code (and algorithms) at rosettacode.org. The maintainer of COBOL has also contributed some BaCon bits, as I recall. It is a useful site, so thanks for the reminder and link. With kind regards, vovchik
|
|
|
Post by bigbass on Aug 11, 2018 18:28:04 GMT 1
Hello vovchik Yes , a lot of code we need to connect the URL's from rosetta to the bacon documentation page for quick references to valid tested code demos lower to upper I couldn't get the syntax to work ? doesn't work as expected s$ = "welcome to bacon" PRINT "Output: ", REPLACE$(s$, "[[:lower:]]" , "[[:upper:]]", 2) Joe here is another way around the REGEX problem and the REPLACE$ with the 2 option
az$ ="abcdefghijklmnopqrstuvwxyz" AZ$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" s$ = "welcome to bacon"
PRINT "Output: ", REPLACE$(s$, az$ , AZ$, 2)
this works though with REGEX with the 1 option s$ = "welcome 2 bacon" PRINT "Output: ", REPLACE$(s$, "[[:digit:]]", "to", 1)
official rosettacode.org/wiki/Tokenize_a_string#BaConand my bacon cheat below to tokenize tr knows nothing about arrays, so this solution only changes each comma to a period. echo 'Hello,How,Are,You,Today' | tr ',' '.' '--- easy tokenize using the 2 option that allows translations '--- similar to the linux tr command
s$ = "Hello,How,Are,You,Today" PRINT "Output: ", REPLACE$(s$, "," , ".", 2)
|
|
|
Post by Pjot on Aug 11, 2018 19:54:59 GMT 1
Hi bigbass, Regarding your question: 'lower to upper I couldn't get the syntax to work ?' The example does not work because only the needle argument allows a regex. See also the REPLACE$ function documentation. Regarding the delimited string, if you want to change the delimiter symbol, it is better to use DELIM$ instead. This function will skip any escaped delimiters properly. Let me show you the difference: s$ = "\"Hello,How\",Are,You,Today" PRINT "Using REPLACE$: ", REPLACE$(s$, "," , " ", 2) PRINT "Using DELIM$: ", DELIM$(s$, "," , " ")
Output: Because one comma occurs between double quotes, it will be excluded from the operation. Best regards Peter
|
|
|
Post by bigbass on Aug 11, 2018 21:47:42 GMT 1
Hello Peter thanks for the DELIM$ example and the documentation page of DELIM It wasn't a problem I was just experimenting porting some tr command line snippets www.folkstalk.com/2012/10/translate-tr-command-examples-in-unix.htmland realized that when using REPLACE$ with regx it will work with flag 1 set to some degree however when using REPLACE$ with regx we can't use regx with flag 2 set as in tr just wanted to clear up that tr and REPLACE$ are similar but showing that regx can't be used with the flag 2 set and showed a workaround Joe
|
|
|
Post by vovchik on Aug 13, 2018 12:18:17 GMT 1
Dear all,
I quite like this mathematical solution suggested on Rosetta Code by LinearSpoon (originally for AutoHotkey):
' ------------------ FUNCTION ROT13$(char* s) ' ------------------ LOCAL o$ TYPE STRING LOCAL a, i TYPE int FOR i = 0 TO LEN(s) - 1 a = ~s[i] o$ = o$ & CHR$(~a - 1 / (~(a | 32) / 13 * 2 - 11) * 13) NEXT i RETURN o$ END FUNCTION
PRINT ROT13$("Basic") PRINT ROT13$("Bas,ic") x$ = "OnPba vf Terng" PRINT ROT13$(x$)
With kind regards, vovchik
|
|
|
Post by bigbass on Aug 13, 2018 17:22:36 GMT 1
Hello vovchik Its nice to see that we can do binary level ones complements and binary shifting in bacon as a porting challenge you got it done and it is probably the fastest based on the lowest possible math of shifting binary bits but I still prefer the regex you did because it could be modified easily for other rotations by changing the regex pattern the math formula I would have to work it out the old way with a pencil first to see how to modify it for some other conversion The last time I did that type of binary conversion was when I sent pulses to a stepper motor from the parallel port but what I did was convert everything to decimal later so I could remember what to change to make it somewhat readable thanks it is still better to have more options to choose from Joe
|
|