android.util.Base64 编码/解码标志参数

发布于 2025-01-08 12:27:51 字数 704 浏览 3 评论 0原文

根据 Javadoc,android.util.Base64.decode() 采用两个参数:文本和“flags”。这些标志采用 int 形式并且(我引用):

flags 控制解码输出的某些功能。传递 DEFAULT 来解码标准 Base64。

首先,感谢决定编写模糊 Javadoc 的人。我看到 Base64 有一些枚举字符串,在实践中,我们一直使用 Base64.NO_WRAP 作为我们的标志。然而,在这个特定的实例中,我们需要使用两个标志:NO_WRAP 和 URL_SAFE。

我们如何指定这两个标志?我们尝试用管道(“|”)将它们分开,但没有成功。

import android.util.Base64;
public class Foo {
    public static void main(String[] args) {
        String urlSafeBase64EncodedString = getUrlSafeBase64EncodedString();
        int flags = ????????; //Need both Base64.NO_WRAP and Base64.URL_SAFE
        String decodedString = Base64.decode(urlSafeBase64EncodedString, flags);
    }
}

感谢您抽出时间。

According to the Javadoc, android.util.Base64.decode() takes two parameters: the text, and "flags". These flags are in int form and (I quote):

flags controls certain features of the decoded output. Pass DEFAULT to decode standard Base64.

First off, thanks to whomever decided to write a vague Javadoc. I see that Base64 has some enumeration strings, and in practice, we have been using Base64.NO_WRAP as our flag. In this particular instance, however, we need to employ two flags: NO_WRAP, and URL_SAFE.

How do we specify both flags? We tried separating them with a pipe ('|'), and that didn't do it.

import android.util.Base64;
public class Foo {
    public static void main(String[] args) {
        String urlSafeBase64EncodedString = getUrlSafeBase64EncodedString();
        int flags = ????????; //Need both Base64.NO_WRAP and Base64.URL_SAFE
        String decodedString = Base64.decode(urlSafeBase64EncodedString, flags);
    }
}

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好听的两个字的网名 2025-01-15 12:27:51

传递给函数的 int 标志通常定义为按位或运算以实现组合效果。

您通常可以通过查看常量值来判断,它们会变成 0 1 2 4 8 16 ..

至于您的问题,您可以使用以下内容进行标志定义:

int flags = Base64.NO_WRAP | Base64.URL_SAFE;

int flags passed to functions are usually defined to be bitwise ORed to achieve a combined effect.

You can usually tell by looking at the constant values, they would go 0 1 2 4 8 16 ..

As for your question you can use the following for your flag definition:

int flags = Base64.NO_WRAP | Base64.URL_SAFE;
逆夏时光 2025-01-15 12:27:51

对于 kotlin 你可以使用

val flag = Base64.URL_SAFE or Base64.NO_WRAP

For kotlin you can use

val flag = Base64.URL_SAFE or Base64.NO_WRAP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文