|
Post by bigbass on Aug 13, 2018 19:35:17 GMT 1
Hello vovchik
Just to have a simple tool for all these low level conversions
I expanded the regex code and did some mental math conversions to get the base64 index number a different way using ascii
I saw that Peter already did a base64 I wanted to focus on conversions of ascii
Joe
' ------------------ FUNCTION INDEX64$(STRING s$) ' ------------------ LOCAL i, ch ,bin 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-J]") THEN DECR ch, 65 PRINT "index to Base64 = " , ASC(ch$-65) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin)
ELIF REGEX(ch$, "[K-Z]") THEN DECR ch, 65 PRINT "index to Base64 = " , ASC(ch$-65) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin) ELIF REGEX(ch$, "[a-z]") THEN DECR ch, 71 PRINT "index to Base64 = " , ASC(ch$-71) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin)
ELIF REGEX(ch$, "[0-9]") THEN INCR ch, 4 PRINT "index to Base64 = " , ASC(ch$+4) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin)
ELIF ch$ = "+" THEN INCR ch, 19 PRINT "index to Base64 = " , ASC(ch$+19) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin)
ELIF ch$ = "/" THEN INCR ch, 16 PRINT "index to Base64 = " , ASC(ch$+16) PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin)
ELIF ch$ = " " THEN INCR ch, 0 PRINT "SPACE " PRINT "ascii = ", ASC(ch$) bin = ASC(ch$) PRINT "binary = " , BIN$(bin) PRINT "hex = " , HEX$(bin) END IF o$ = o$ & CHR$(ch) NEXT i RETURN o$ END FUNCTION
REPEAT INPUT "Enter string to code/decode: ", my_str$ PRINT INDEX64$(my_str$) UNTIL FALSE
|
|
|
Post by Pjot on Aug 13, 2018 21:03:04 GMT 1
The calculation code from vovchik is inspiring, and I cannot beat it. This is my contribution using IIF$ and BETWEEN:
a$ = "This is a string"
WHILE i < LEN(a$) x = PEEK(a$+i) PRINT IIF$((x&31) BETWEEN 1;13,CHR$(x+13),IIF$((x&31)-13 BETWEEN 1;13,CHR$(x-13),CHR$(x))); INCR i WEND
BR Peter
|
|
|
Post by bigbass on Aug 14, 2018 7:13:55 GMT 1
Hello guys Yes, vovchik has added many excellent options to this thread I also like the RANGE example not trying to pick the best they all have something special that is very useful Peter's example with BETWEEN is another built for speed thanks P.S there isn't any example on the rosetta page for base64 for BASIC rosettacode.org/wiki/Base64_encode_dataI saw your base64 demo on the bacon website only Joe
|
|
|
Post by vovchik on Aug 14, 2018 10:40:34 GMT 1
Dear Peter, Thanks for the compliment. I checked your very terse IIF$/BETWEEN version and discovered that it did not take into account chars that were out of the alpha range - chars which should just be passed through the function without change. This version does that, but introduces a REGEX. There must be a way to golf it a bit more via an outer IIF$ rather than IF/THEN/ELSE. DECLARE x$ TYPE STRING
' ------------------ FUNCTION ROT13$(STRING a$) ' ------------------ LOCAL i, x TYPE int LOCAL o$, ch$ TYPE STRING WHILE i < LEN(a$) x = PEEK(a$+i) IF REGEX(CHR$(x), "[A-Ma-mN-Zn-z]") THEN ch$ = IIF$((x&31) BETWEEN 1;13, CHR$(x + 13), \ IIF$((x&31) - 13 BETWEEN 1;13, CHR$(x - 13), CHR$(x))) ELSE ch$ = CHR$(x) END IF INCR i o$ = o$ & ch$ WEND RETURN o$ END FUNCTION
x$ = "Guvf vf n fgevat" PRINT ROT13$(x$) PRINT ROT13$("Basic") PRINT ROT13$("Bas,ic") x$ = "OnPba vf Terng!" PRINT ROT13$(x$) With kind regards, vovchik
|
|
|
Post by vovchik on Aug 14, 2018 23:55:59 GMT 1
Dear all,
We can avoid a second string var for output by using poke, I think.
DECLARE x$ TYPE STRING
' ------------------ FUNCTION ROT13$(STRING a$) ' ------------------ LOCAL i, x TYPE int LOCAL ch TYPE char FOR i = 0 TO LEN(a$) - 1 x = PEEK(a$ + i) IF REGEX(CHR$(x), "[A-Ma-mN-Zn-z]") THEN ch = IIF((x&31) BETWEEN 1;13, (x + 13), \ IIF((x&31) - 13 BETWEEN 1;13, (x - 13), x)) ELSE ch = x END IF POKE a$ + i, ch NEXT i RETURN a$ END FUNCTION
x$ = "Guvf vf n fgevat" PRINT ROT13$(x$) PRINT ROT13$("Basic") PRINT ROT13$("Bas,ic") x$ = "OnPba vf Terng!" PRINT ROT13$(x$)
With kind regards, vovchik
|
|
|
Post by vovchik on Aug 15, 2018 10:06:55 GMT 1
Dear all,
And we can do the strictly computational version using PEEK/POKE, too (thanks Peter!), for even more terseness:
OPTION EXPLICIT TRUE DECLARE x$ TYPE STRING
' ------------------ FUNCTION ROT13$(STRING s$) ' ------------------ LOCAL i TYPE int LOCAL a TYPE char FOR i = 0 TO LEN(s$) - 1 a = ~PEEK(s$ + i) POKE s$ + i, ~a - 1 / (~(a | 32) / 13 * 2 - 11) * 13 NEXT i RETURN s$ END FUNCTION
x$ = "Guvf vf n fgevat" PRINT ROT13$(x$) PRINT ROT13$("Basic") PRINT ROT13$("Bas,ic") x$ = "OnPba vf Terng!" PRINT ROT13$(x$)
With kind regards, vovchik
|
|
|
Post by Pjot on Aug 16, 2018 19:01:25 GMT 1
Hi vovchik, Well, I give up - this cannot be done more concise as far as I can see Best regards Peter PS interesting use of the '~' operator, I will add this to the manual.
|
|
|
Post by vovchik on Aug 21, 2018 15:31:14 GMT 1
Dear all,
And while thingking about ROT13, I saw some twitter feeds done in MORSE code. Here is a little function that does that (uppercase ASCII letters, nums and space):
OPTION EXPLICIT TRUE DECLARE x$ TYPE STRING
' ------------------ FUNCTION ASC2MORSE$(STRING s$) ' ------------------ LOCAL o$, asc_morse$[2] TYPE STRING LOCAL i, f TYPE int s$ = UCASE$(s$) asc_morse$[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " asc_morse$[1]=".-,-...,-.-.,-..,.,..-.,--.," \ "....,..,.---,-.-,.-..,--,-.," \ "---,.--.,--.-,.-.,...,-,..-,...-," \ ".--,-..-,-.--,--..,.----,..---,...--," \ "....-,.....,-....,--...,---..,----,-----" FOR i = 1 TO LEN(s$) f = INSTR(asc_morse$[0], MID$(s$, i, 1)) o$ = o$ & TOKEN$(asc_morse$[1], f, ",") & " " NEXT i RETURN o$ END FUNCTION
' ------------------ FUNCTION MORSE2ASC$(STRING s$) ' ------------------ LOCAL o$, asc_morse$[2], ch$ TYPE STRING LOCAL i, f TYPE int asc_morse$[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " asc_morse$[1]=".-,-...,-.-.,-..,.,..-.,--.," \ "....,..,.---,-.-,.-..,--,-.," \ "---,.--.,--.-,.-.,...,-,..-,...-," \ ".--,-..-,-.--,--..,.----,..---,...--," \ "....-,.....,-....,--...,---..,----,-----" FOR i = 1 TO AMOUNT(s$, " ") ch$ = TOKEN$(s$, i, " ") f = ISTOKEN(asc_morse$[1], ch$, ",") IF f = 0 THEN o$ = o$ & " " ELSE o$ = o$ & MID$(asc_morse$[0], f, 1) END IF NEXT i RETURN o$ END FUNCTION
x$ = "A0" PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = "HOW ARE YOU" PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = "BACON IS GREAT " PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = ".- -----" PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$ x$ = ".... --- .-- .- .-. . -.-- --- ..- " PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$ x$="-... .- -.-. --- -. .. ... --. .-. . .- -" PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$
The code probably could use some golfing...
With kind regards, vovchik
|
|
|
Post by vovchik on Aug 22, 2018 0:00:41 GMT 1
Dear all, And provided you have /dev/audio, you can have Morse sounds. ' *********************** ' INITIALIZATION ' ***********************
OPTION EXPLICIT TRUE CONST AMPLITUDE = 120 CONST SAMPLERATE = 8000 DECLARE myfreq, myduration TYPE long DECLARE x$ TYPE STRING
' *********************** ' END INITIALIZATION ' ***********************
' *********************** ' IMPORTS ' ***********************
' System call for console environment IMPORT "ioctl" FROM "libc.so" TYPE int ALIAS IOCTL ' System open file IMPORT "open(char*,int,int)" FROM "libc.so" TYPE int ALIAS SYSOPEN ' System close file IMPORT "close(int)" FROM "libc.so" TYPE int ALIAS SYSCLOSE 'write(audio_fd, audio_buffer, buf_size) IMPORT "write(int,int)" FROM "libc.so" TYPE int ALIAS SYSWRITE
' *********************** ' END IMPORTS ' ***********************
' *********************** ' SUBS & FUNCTIONS ' ***********************
' ----------------- FUNCTION RANGE(double v0, double v1, double v2) ' ----------------- ' golfed RETURN IIF(v0 >= MIN(v1, v2) AND v0 <= MAX(v1, v2), 1, 0) END FUNCTION
' ----------------- SUB BLEEP(long freq, long duration) ' ----------------- LOCAL i, fd, z TYPE int LOCAL x[1] TYPE unsigned char LOCAL DURATION TYPE float DURATION = (float)duration / 1000.0 fd = SYSOPEN("/dev/dsp", 1, 0) FOR i = 0 TO (DURATION * SAMPLERATE) x[0] = 128 + AMPLITUDE * SIN((double)i/SAMPLERATE * freq * (2 * PI)) z = SYSWRITE(fd, x, SIZEOF(x)) NEXT i SYSCLOSE(fd) END SUB
' ------------------ FUNCTION ASC2MORSE$(STRING s$) ' ------------------ LOCAL o$, asc_morse$[2] TYPE STRING LOCAL i, f TYPE int s$ = UCASE$(s$) asc_morse$[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " asc_morse$[1]=".-,-...,-.-.,-..,.,..-.,--.," \ "....,..,.---,-.-,.-..,--,-.," \ "---,.--.,--.-,.-.,...,-,..-,...-," \ ".--,-..-,-.--,--..,.----,..---,...--," \ "....-,.....,-....,--...,---..,----,-----" FOR i = 1 TO LEN(s$) f = INSTR(asc_morse$[0], MID$(s$, i, 1)) o$ = o$ & TOKEN$(asc_morse$[1], f, ",") & " " NEXT i RETURN o$ END FUNCTION
' ------------------ FUNCTION MORSE2ASC$(STRING s$) ' ------------------ LOCAL o$, asc_morse$[2], ch$ TYPE STRING LOCAL i, f TYPE int asc_morse$[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " asc_morse$[1]=".-,-...,-.-.,-..,.,..-.,--.," \ "....,..,.---,-.-,.-..,--,-.," \ "---,.--.,--.-,.-.,...,-,..-,...-," \ ".--,-..-,-.--,--..,.----,..---,...--," \ "....-,.....,-....,--...,---..,----,-----" FOR i = 1 TO AMOUNT(s$, " ") ch$ = TOKEN$(s$, i, " ") f = ISTOKEN(asc_morse$[1], ch$, ",") IF f = 0 THEN o$ = o$ & " " ELSE o$ = o$ & MID$(asc_morse$[0], f, 1) END IF NEXT i RETURN o$ END FUNCTION
' ------------------ SUB MORSEBLEEP(STRING s$) ' ------------------ LOCAL i TYPE int LOCAL ch$ TYPE STRING FOR i = 1 TO LEN(s$) ch$ = MID$(s$, i, 1) SELECT ch$ CASE "." BLEEP(600, 200) CASE "-" BLEEP(1000, 400) CASE " " BLEEP(40, 600) END SELECT NEXT i END SUB
' *********************** ' END SUBS & FUNCTIONS ' ***********************
' *********************** ' MAIN ' ***********************
x$ = "A0" PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = "HOW ARE YOU" PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = "BACON IS GREAT " PRINT "ASC2MORSE: ", x$, NL$, ASC2MORSE$(x$), NL$ x$ = ".- -----" PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$ MORSEBLEEP(x$) x$ = ".... --- .-- .- .-. . -.-- --- ..- " PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$ MORSEBLEEP(x$) x$="-... .- -.-. --- -. .. ... --. .-. . .- -" PRINT "MORSE2ASC: ", x$, NL$, MORSE2ASC$(x$), NL$ MORSEBLEEP(x$)
' *********************** ' END MAIN ' ***********************
With kind regards, vovchik
|
|
|
Post by Pjot on Aug 22, 2018 18:59:05 GMT 1
Thanks vovchik! Your program made me think. On some local TV channels we can see the news which then is "translated" by somebody using sign language. Would it not be interesting to create a translation program generating sign symbols using SVG, for example?
Such program would indeed be very useful, and I wonder if a sign language translator exists at all...
Best regards Peter
|
|
|
Post by vovchik on Aug 22, 2018 19:36:02 GMT 1
Dear Peter,
What a wonderful idea. It should be pretty easy to do, on the basis of the ASC2MORSE$ function or something like it. I will try to find a table with sign language symbols.
With kind regards, vovchik
|
|
|
Post by vovchik on Aug 22, 2018 19:54:15 GMT 1
Dear Peter, I did not find a set of svgs right away, but was able to find a set of png files here abc and used potrace to convert them to nice, little svg files (archive attached). Now have to think about how one might do this properly. With kind regards, vovchik Attachments:svg_20180822.tar.gz (14.76 KB)
|
|
|
Post by vovchik on Aug 22, 2018 23:30:09 GMT 1
Dear all, Here is a little sign language test that generates svgs from ascii text. Just compile and run. You will see some svgs, I hope. Still some work to be done to generate a composite svg, but I think we have the means now. With kind regards, vovchik Attachments:asc2asl-src.tar.gz (14.56 KB)
|
|
|
Post by bigbass on Aug 23, 2018 17:14:14 GMT 1
Hello vovchik Having many types of encoders in compact functions can solve a lot problems having the bacon syntax that compiles is excellent for demo code / small apps Thanks again for all your work looks very interesting Joe
|
|
|
Post by Pjot on Aug 23, 2018 19:20:27 GMT 1
Thanks vovchik,
That looks really promising! The program compiles and runs without issues. I think it is actually a very useful program.
Best regards Peter
|
|