本文介绍了android.util.Base64编码/解码标志参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Javadoc,android.util.Base64.decode()具有两个参数:text和"flags".这些标志为int形式,并且(我引用):

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.

首先,要感谢决定编写模糊的Javadoc的人.我看到Base64有一些枚举字符串,实际上,我们一直在使用Base64.NO_WRAP作为我们的标志.但是,在这种情况下,我们需要使用两个标志:NO_WRAP和URL_SAFE.

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);
    }
}

感谢您的时间.

推荐答案

int标志定义为按位或,以实现组合效果.

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

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

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;

这篇关于android.util.Base64编码/解码标志参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 22:38