首先,我们来看一个例子:
byte[] b=new byte[]{2,9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.getbytes();
这种情况下,b和b1字节数组是相同的。
那下面这种情况呢?
byte[] b=new byte[]{-2,-9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.getbytes();
打印出来的ss是一堆我们看不懂的东西!而且我们发现b和b1字节数组长度都不同啦?为什么?
我们知道ascii编码的范围为0~127,那么-2,-9该如何编码呢?
b1和b的字节表示在传递过程中,数据失真了,那如何解决失真问题呢?
我们可以使用base64对-128~127的值进行改造(具体请自行google之)。
通过使base64编码解码则可以防止传输过程中出错。base64可使用commons-codec的,如下所示:
Method Summary
Modifier and Type | Method and Description |
---|---|
static byte[] |
decodeBase64(byte[] base64Data)
Decodes Base64 data into octets
|
static byte[] |
decodeBase64(String base64String)
Decodes a Base64 String into octets
|
static BigInteger |
decodeInteger(byte[] pArray)
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
|
static byte[] |
encodeBase64(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.
|
static byte[] |
encodeBase64(byte[] binaryData, boolean isChunked)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
|
static byte[] |
encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
|
static byte[] |
encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
|
static byte[] |
encodeBase64Chunked(byte[] binaryData)
Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
|
static String |
encodeBase64String(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.
|
static byte[] |
encodeBase64URLSafe(byte[] binaryData)
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
|
static String |
encodeBase64URLSafeString(byte[] binaryData)
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
|
static byte[] |
encodeInteger(BigInteger bigInt)
Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
|
static boolean |
isArrayByteBase64(byte[] arrayOctet)
Deprecated.
1.5 Use
isBase64(byte[]) , will be removed in 2.0. |
static boolean |
isBase64(byte octet)
Returns whether or not the
octet is in the base 64 alphabet. |
static boolean |
isBase64(byte[] arrayOctet)
Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
|
static boolean |
isBase64(String base64)
Tests a given String to see if it contains only valid characters within the Base64 alphabet.
|
protected boolean |
isInAlphabet(byte octet)
Returns whether or not the
octet is in the Base64 alphabet. |
boolean |
isUrlSafe()
Returns our current encode mode.
|
注意,当url传输过程中,为了保证不传输错误(例如缺少“+”等),请尽量使用urlSafe方法。
byte[] b=new byte[]{-2,-9,43}; byte[] s=Base64.encodeBytesToBytes(b); byte[] b1=Base64.decode(s);
我们看一下编码后的s是什么样子的?
47, 118, 99, 114
编码后全部变为0~127的ascii编码,解码后b1的值为:
-2, -9, 43
b和b1相同,没有数据失真。
另外,也可以是使用bouncy castle支持。具体可以google之。
一些小细节:
1. 跨平台传输时可能传输的是十六进制字符串,要转换为byte数组再进行编码,转换方法为:从高位开始,两个十六进制字符为一组转为byte。实例如下:
String hex="1a2bcc";
先拆分,把“1a”,“2b” “cc”分别解析为byte数组 26,43,208
2. 跨平台要考虑编码格式,如utf-8 或者gbk 或者iso-8895-1等。