Question

I'm trying to write a customization in Lyris ListManager (10.2). The language is TCL, which I know very little about. We need to encode a value as base64 (or really, anything that obfuscates a querystring parameter), but I can't seem to figure out how. Is there a command native to TCL to do this?

Was it helpful?

Solution

Following your problem to use the base64 package you can use these little procs to convert your data to hex and back. Requires Tcl > 8

proc BIN2HEX { text }   { binary scan $text H* result; return $result }
proc HEX2BIN { hex }    { return [binary format H* $hex] }
set hex [BIN2HEX $yourText]
set textAgain [HEX2BIN $hex]

If you really need base64 you can just copy/paste the entire base64 file from the tcllib distribution http://sourceforge.net/projects/tcllib/files/tcllib/1.11.1/ into your code (remove the "package provides" line)

OTHER TIPS

The existence of http://tcllib.sourceforge.net/doc/base64.html seems to indicate that there're no native functions. You could copy the source and add it to your modifications.

% base64::encode "Hello, world"
SGVsbG8sIHdvcmxk

% base64::encode [string repeat xyz 20]
eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6
eHl6eHl6eHl6
% base64::encode -wrapchar "" [string repeat xyz 20]
eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6eHl6

# NOTE: base64 encodes BINARY strings.
% set chemical [encoding convertto utf-8 "C\u2088H\u2081\u2080N\u2084O\u2082"]
% set encoded [base64::encode $chemical]
Q+KCiEjigoHigoBO4oKET+KCgg==
% set caffeine [encoding convertfrom utf-8 [base64::decode $encoded]]

If you have the ability to load Tcl libraries (packages/modules) into your environment, you can just use the Tcllib implementation. That's what Vinko Vrsalovic was showing the command from in his response.

% package require base64
2.4
% base64::encode bob
Ym9i

If it would be good enough to just encode in hexadecimal, you can use the binary command as follows:

% set query "Hello, world"
Hello, world
% binary scan $query H* hexquery
1
% puts $hexquery
48656c6c6f2c20776f726c64
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top