我想要像“string”这样简单的东西 - > BASE64。使用较旧的base64.encode-str很容易(并且听起来更“讨厌”,但是较新的clojure.data.codec.base64
需要输入和输出流,而且似乎是一种围绕Java做事方式的丑陋包装。
那么,有一个字符串,获取base64编码数组的方式是什么?谢谢
答案 0 :(得分:22)
四年后,但我认为如果您的 JDK 1.8 或更高,这值得一提。它只使用java.util.Base64
(:import java.util.Base64)
(defn encode [to-encode]
(.encode (Base64/getEncoder) (.getBytes to-encode)))
(:import java.util.Base64)
(defn encode [to-encode]
(.encodeToString (Base64/getEncoder) (.getBytes to-encode)))
(:import java.util.Base64)
(defn decode [to-decode]
(String. (.decode (Base64/getDecoder) to-decode)))
答案 1 :(得分:17)
另一个答案还需要一个步骤:将encode
的字节数组结果转换为字符串。这是我的所作所为:
(:require [clojure.data.codec.base64 :as b64])
(defn string-to-base64-string [original]
(String. (b64/encode (.getBytes original)) "UTF-8"))
答案 2 :(得分:10)
您可以使用encode函数并传递字节数组:
(encode (.getBytes "Hello world!"))
答案 3 :(得分:3)
ztellman/byte-transforms也支持base64编码/解码。
(encode "hello" :base64 {:url-safe? true})
答案 4 :(得分:1)
Clojure equivalent of python's base64 encode and decode
可能重复The Tupelo library包含基于Java Base64和Base64Url功能的Clojure包装器。查看单元测试会显示代码的实际效果:
(ns tst.tupelo.base64
(:require [tupelo.base64 :as b64] ))
code-str (b64/encode-str orig)
result (b64/decode-str code-str) ]
(is (= orig result))
输入&输出值是普通字符串(字节数组也有变体)。
API文档are here.
答案 5 :(得分:1)
对于那些尝试(像我一样)将图像转换为数据URI(用于在HTML中嵌入图像)的人:
<TextView
android:text="00:00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="50dp"
android:textStyle="bold"
android:textColor="#009688"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="@+id/button" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/button3" />
<Button
android:text="Save Lap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="@+id/listview1"/>
<TextView
android:id="@+id/textViewBatteryStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/button2"
android:layout_below="@+id/textView"
android:text="Current Battery Status"
android:textAppearance="?android:attr/textAppearanceLarge" />