I have decided to discard using this script in my platform. This is an ML generated script which I then used as an learning exercise. As lovely it would be to use it I have a hunch of that I shouldn't so I won't. Prior to this statement, the below was two-months ago and now void.
I feel that some philosophical questions should be asked. If that ML generated code is allowed or not?
If TCL is to gain some popularity than ML will have to be used like it or not which is an dilemma as it's pollutes quality and control.
I came across this page: https://www.kuon.ch/post/2020-02-27-base24/ about encoding bytes to their own Base24 formula for such case examples as recovery codes making them reliable and easier for the user.
Unfortunately, I have no such knowledge of how bit-shifting works, what it was, and what you would need to do to produce such algorithm in TCL but now I do thanks to ML. This would of been tricky to explain over IRC with many questions asked, may of worked over conference but time is limited.
Everyone has their own stance on ML for coding, whether it's right or wrong is up to one ethics, but I decided to be lazy and that I would provide a pre-made JavaScript equivalent from the author and get the model to convert it to TCL and understand it that way. What I throw in ASCII appears to encode to Base24 format and vice versa; nifty.
% set decoded_bytes [decode24 "FGTYXEPEPXG8XYC9FZAG43WS8B2B"] ;# TCL Lang is COOL 84 67 76 32 76 97 110 103 32 105 115 32 67 79 79 76 % set decoded_text [bytes_to_ascii $decoded_bytes] TCL Lang is cool
The caveat to this is that this isn't my code, a unix admin who doesn't get on with any other languages other than perl. Languages are expanding too fast with rapidly growing eco-systems so you never know what their code is doing anyway. I find reading C/++ interesting but only ever dabbled in it. I'm someone who enjoys reinventing the wheel because other wheels don't support my requirements.
I also I don't want to be someone who at drop of a hat uses ML and cobbles together the code generated. I do write my own code in my own happy flawed logic and I do believe there is no right or wrong to code. If your syntax produces the result you want reliably personally then what's wrong? But, this was something new to learn and that I now understand the logic behind it, it's not actually that hard. It's cute.
So in return to grant myself permission to use it, I want to prove to that I at least understand the workings of what's actually occurring. Since my personal views are of that this is no different than copying from StackOverflow or some ancient forum found on the 56th page of google that the model has probably been trained off anyway.
If this is the darkest sin of the TCL; please feel free to yell and I'll abolish the code to the delete key. If there was a license it would be ML-Open-Domain, created by ML, I don't know maybe I should ask it. Anyway enough babble and moral judgement, the code sits below with my own comments inline.
# Base24 alphabet and size of alphabet set ALPHABET "ZAC2B3EF4GH5TK67P8RS9WXY" set ASIZE [string length $ALPHABET] # Create decode map array set DECODE_MAP {} for {set i 0} {$i < $ASIZE} {incr i} { # Set the char to the index of i set char [string index $ALPHABET $i] # convert the letter to lowercase and index the character set DECODE_MAP([string tolower $char]) $i # index the character set DECODE_MAP($char) $i } # Convert ASCII string to bytes proc ascii_to_bytes {text} { set bytes {} foreach char [split $text ""] { # convert each character to bytes lappend bytes [scan $char %c] } return $bytes } # Convert bytes back to ASCII string proc bytes_to_ascii {bytes} { set ascii_string "" foreach byte $bytes { # concert the character back to byte append ascii_string [format %c $byte] } return $ascii_string } proc encode24 {data} { global ALPHABET ASIZE # Check input length set len [llength $data] # if the data providded isn't a set of four bytes than bail if {$len % 4 != 0} { error "Data length must be a multiple of 4" } # construct an empty list for our result set result {} for {set i 0} {$i < $len} {incr i 4} { # Get 4 bytes and combine them into 32-bit value # Get the first four bytes # Get the second set of four bytes # Get the third set of four bytes # Get the forth set of four bytes # Example string: TCL -- 84 67 76 # b3 = 84 # b2 = 67 # b1 = 76 # b0 = 00 (we pad 00 to make it a 4bit byte somehow) set b3 [lindex $data $i] puts "$b3 - b3 --- [bytes_to_ascii $b3]" set b2 [lindex $data [expr {$i + 1}]] puts "$b2 - b2 --- [bytes_to_ascii $b2]" set b1 [lindex $data [expr {$i + 2}]] puts "$b1 - b1 --- [bytes_to_ascii $b1]" set b0 [lindex $data [expr {$i + 3}]] puts "$b0 - b0 --- [bytes_to_ascii $b0]" # Combine bytes into 32-bit value set value [expr { (($b3 & 0xFF) << 24) | (($b2 & 0xFF) << 16) | \ (($b1 & 0xFF) << 8) | ($b0 & 0xFF)}] puts "$value -- main value after bit shifting all eight bytes ---" puts "[format %x $value] - Bitshift value in hex" # b3 = xxxxxxxx 00000000 00000000 00000000 (<< 24) # b2 = 00000000 xxxxxxxx 00000000 00000000 (<< 16) # b1 = 00000000 00000000 xxxxxxxx 00000000 (<< 8) # b0 = 00000000 00000000 00000000 xxxxxxxx # The value is represented in decmical but if we were to convert to hex # puts "[format %x [expr (($b3 & 0xFF) << 24)]] - Expreession" # Convert to base24 set subResult {} for {set k 0} {$k < 7} {incr k} { ;# Loop 8 times set idx [expr {$value % $ASIZE}] ;# Get remainder by dividing the value by the size of the alpahbet puts "$idx -- value modulo of the dictonary size ---" set value [expr {$value / $ASIZE}] ;# Divide the decmical value from above by the alphabet size puts "$value -- demical value divided by the size of alphabet ---" set subResult [string index $ALPHABET $idx]$subResult ;# Get the letter from our alphabet based on the index with our remainder puts "$subResult -- subResult" ;#Append our next character to the front of our previous characters puts "------" } append result $subResult } return $result } proc decode24 {data} { global ASIZE DECODE_MAP # Check input length set len [string length $data] if {$len % 7 != 0} { error "Data length must be a multiple of 7" } set bytes {} for {set i 0} {$i < $len} {incr i 7} { set subData [string range $data $i [expr {$i + 6}]] set value 0 # Convert from base24 foreach char [split $subData ""] { if {![info exists DECODE_MAP($char)]} { error "Undefined character in input: $char" } set value [expr {$ASIZE * $value + $DECODE_MAP($char)}] } # Extract bytes lappend bytes [expr {($value >> 24) & 0xFF}] lappend bytes [expr {($value >> 16) & 0xFF}] lappend bytes [expr {($value >> 8) & 0xFF}] lappend bytes [expr {$value & 0xFF}] } return $bytes } # Example usage: # Encoding ASCII text set text "TCL is cool" set bytes [ascii_to_bytes $text] # Pad to multiple of 4 while {[llength $bytes] % 4 != 0} { lappend bytes 0 } set encoded [encode24 $bytes] # Decoding back to ASCII set decoded_bytes [decode24 "$encoded"] set decoded_text [bytes_to_ascii $decoded_bytes] puts [string trimright $decoded_text \0]
The output is long, so I've provided a snip
9 -- value modulo of the dictonary size --- 0 -- demical value divided by the size of alphabet --- GRSACPZ -- subResult ------ encoded: FGTYXEPGEB4WT2GRSACPZ TCL is cool