将值编码为base64

时间:2009-12-02 15:57:25

标签: base64 tcl encode

我正在尝试在Lyris ListManager(10.2)中编写自定义。语言是TCL,我对此知之甚少。我们需要将值编码为base64(或者实际上,任何混淆查询字符串参数的东西),但我似乎无法弄清楚如何。是否有TCL原生的命令来执行此操作?

4 个答案:

答案 0 :(得分:2)

http://tcllib.sourceforge.net/doc/base64.html的存在似乎表明没有本地功能。您可以复制源并将其添加到您的修改中。

% 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]]

答案 1 :(得分:2)

在您使用base64软件包时遇到问题,您可以使用这些小程序将数据转换为十六进制和返回。需要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]

如果你真的需要base64,你可以将整个base64文件从tcllib发行版http://sourceforge.net/projects/tcllib/files/tcllib/1.11.1/复制/粘贴到你的代码中(删除“包提供”行)

答案 2 :(得分:1)

如果您能够将Tcl库(包/模块)加载到您的环境中,则可以使用Tcllib实现。这就是Vinko Vrsalovic在回应中表达出来的命令。

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

答案 3 :(得分:1)

如果仅以十六进制编码就足够了,可以使用binary命令,如下所示:

% set query "Hello, world"
Hello, world
% binary scan $query H* hexquery
1
% puts $hexquery
48656c6c6f2c20776f726c64