Clojure等同于python的base64编码和解码 [英] Clojure equivalent of python's base64 encode and decode
本文介绍了Clojure等同于python的base64编码和解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个python代码片段,需要帮助与clojure等效。
I have this python code snippet and need help with a clojure equivalent.
user_id = row.get('user_id')
if user_id:
user_id_bytes = base64.urlsafe_b64decode(user_id)
creation_timestamp = int.from_bytes(user_id_bytes[:4],
byteorder='big')
dc_id = int.from_bytes(user_id_bytes[4:5], byteorder='big') & 31
if creation_timestamp > WHEN_WE_SET_UP_DC_IDS:
row['dc_id'] = dc_id}
推荐答案
您可以使用clojure的java兼容性来利用 java.util.Base64 类。
You can use clojure's java compatibility to leverage the java.util.Base64 class.
user> (import java.util.Base64)
java.util.Base64
user> ;; encode a message
(let [message "Hello World!"
message-bytes (.getBytes message)
encoder (Base64/getUrlEncoder)]
(.encodeToString encoder message-bytes))
"SGVsbG8gV29ybGQh"
user> ;; Decode a message
(let [encoded-message "SGVsbG8gV29ybGQh"
decoder (Base64/getUrlDecoder)]
(String. (.decode decoder encoded-message)))
"Hello World!"
这篇关于Clojure等同于python的base64编码和解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文