Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:46 29 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Promote for Base 64 Coded Strings and Data Transfer

     Page 2 of 2    
Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6102
Posted: 02:15am 17 Mar 2023
Copy link to clipboard 
Print this post

If nothing else, Base64 makes the transfer 30% bigger = 30% slower.
VK7JH
MMedit   MMBasic Help
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2080
Posted: 02:42am 17 Mar 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  Also, every additional command is something that 90% of users will never use


precisely! give me a good set of really optimized primitives all day long - I'll build everything else.

Thing is as well, beyond some fringe function being used by a tiny percentage of users; it will only appear once in the code too.

I think there are some things missing from MMBasic but I quite enjoy the canter to create them - many of my own published functions are labelled "VB-work-a-like"

I was looking at PeterMs "is this too silly" thread and in some reading-around, I found a thread from a few years back (discussing +=, -= etc.) and he said "It isn't BASIC" - that struck me because he's spot on ... x=x+1 is bread and butter BASIC. Yes I get the performance nudges of the number of things in an expression and the number of times you call up a variable etc. but if you are into stuff that breaks the accepted constructs and syntax then find a language that gives what you want - C has just about everything you could need but I find it largely unfathomable, so i stick with what I like. It is what it is. Of course there are things that break this (<< and >>) and I use them. I wrestle with my hypocrisy.
Edited 2023-03-17 12:45 by CaptainBoing
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6814
Posted: 08:11am 17 Mar 2023
Copy link to clipboard 
Print this post

TBH I don't have a problem with the shifts. They are often included in loops and are convoluted and awkward to do in ordinary BASIC but lightning fast in code so I think there's a place for them. I do have a bit of a problem with INC, although I do use it. X=X+1 is fine with me. I just don't worry myself with what's going on "under the hood".   :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1115
Posted: 08:55am 17 Mar 2023
Copy link to clipboard 
Print this post

Here are some Program examples of how to encode and decode Base64

Encode

Decode

not im MM Basic but it should be no Rocket Science to convert some of the code-examples to it.

my 2c
Mart!n
Edited 2023-03-17 18:59 by Martin H.
'no comment
 
Martin H.

Guru

Joined: 04/06/2022
Location: Germany
Posts: 1115
Posted: 09:13am 17 Mar 2023
Copy link to clipboard 
Print this post

Here is what CHatGPT answers:

can you write a function in bbc basic to encode something in BASE64?

Sure, here's an example function written in BBC BASIC for encoding a string in BASE64:

DEFPROCbase64encode(str$)
REM BASE64 encoding table
LOCAL b64$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

REM Initialize variables
LOCAL len%, i%, j%, k%, b1%, b2%, b3%, enc$ = ""

REM Determine the length of the input string
len% = LEN(str$)

REM Loop through each three bytes of the input string and encode them
FOR i% = 1 TO len% STEP 3
 b1% = ASC(MID$(str$, i%, 1))
 b2% = IF(i% + 1 <= len%, ASC(MID$(str$, i% + 1, 1)), 0)
 b3% = IF(i% + 2 <= len%, ASC(MID$(str$, i% + 2, 1)), 0)
 j% = b1% DIV 4
 k% = (b1% AND 3) * 16 + b2% DIV 16
 enc$ += MID$(b64$, j% + 1, 1) + MID$(b64$, k% + 1, 1)
 IF b2% > 0 THEN
   j% = (b2% AND 15) * 4 + b3% DIV 64
   k% = b3% AND 63
   enc$ += MID$(b64$, j% + 1, 1) + MID$(b64$, k% + 1, 1)
 ENDIF
NEXT i%

REM Pad the output string with "=" characters if necessary
IF len% MOD 3 = 1 THEN enc$ += "=="
IF len% MOD 3 = 2 THEN enc$ += "="

REM Return the encoded string
ENDPROC enc$

You can call this function by passing in a string to be encoded, like this:
mystring$ = "Hello, world!"
PRINT PROCbase64encode(mystring$)

This should output the BASE64-encoded string for "Hello, world!", which is "SGVsbG8sIHdvcmxkIQ==".
---------

can you also wrIte the function to decode?

Certainly! Here's an example function written in BBC BASIC for decoding a BASE64-encoded string:
DEFPROCbase64decode(enc$)
REM BASE64 decoding table
LOCAL b64$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

REM Initialize variables
LOCAL len%, i%, j%, k%, b1%, b2%, b3%, b4%, dec$ = ""

REM Determine the length of the input string
len% = LEN(enc$)

REM Loop through each four bytes of the input string and decode them
FOR i% = 1 TO len% STEP 4
 b1% = INSTR(b64$, MID$(enc$, i%, 1)) - 1
 b2% = INSTR(b64$, MID$(enc$, i% + 1, 1)) - 1
 b3% = IF(i% + 2 <= len%, INSTR(b64$, MID$(enc$, i% + 2, 1)) - 1, 0)
 b4% = IF(i% + 3 <= len%, INSTR(b64$, MID$(enc$, i% + 3, 1)) - 1, 0)
 j% = (b1% * 4) + (b2% DIV 16)
 k% = ((b2% AND 15) * 256) + (b3% * 4) + (b4% DIV 64)
 dec$ += CHR$(j%) + CHR$(k%)
 IF b3% > 0 THEN
   j% = ((b4% AND 63) * 16) + (b3% DIV 4)
   k% = (b3% AND 3) * 64 + b4%
   dec$ += CHR$(j%)
   IF b4% > 0 THEN dec$ += CHR$(k%)
 ENDIF
NEXT i%

REM Return the decoded string
ENDPROC dec$

You can call this function by passing in a BASE64-encoded string to be decoded, like this:
myencoded$ = "SGVsbG8sIHdvcmxkIQ=="
PRINT PROCbase64decode(myencoded$)

This should output the decoded string for "SGVsbG8sIHdvcmxkIQ==", which is "Hello, world!".
'no comment
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024