首先,咱们来看一个例子:html
byte[] b=new byte[]{2,9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.getbytes();
这种状况下,b和b1字节数组是相同的。java
那下面这种状况呢?apache
byte[] b=new byte[]{-2,-9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.getbytes();
打印出来的ss是一堆咱们看不懂的东西!并且咱们发现b和b1字节数组长度都不一样啦?为何?api
咱们知道ascii编码的范围为0~127,那么-2,-9该如何编码呢? 数组
b1和b的字节表示在传递过程当中,数据失真了,那如何解决失真问题呢?oracle
咱们能够使用base64对-128~127的值进行改造(具体请自行google之)。google
经过使base64编码解码则能够防止传输过程当中出错。base64可以使用commons-codec的,以下所示:编码
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方法。url
byte[] b=new byte[]{-2,-9,43}; byte[] s=Base64.encodeBytesToBytes(b); byte[] b1=Base64.decode(s);
咱们看一下编码后的s是什么样子的?spa
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等。