跨平台传输中使用base64来保证非ascii码字符串的完整性
来源:互联网 发布:新手模拟钢琴软件 编辑:程序博客网 时间:2024/06/16 23:37
首先,我们来看一个例子:
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
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等。
0 0
- 跨平台传输中使用base64来保证非ascii码字符串的完整性
- 简单的ASCII码字符串加密
- python中ASCII码字符与int之间的转换
- (iOS开发)判断字符串中是否含有非ASCII码字符
- PRId64正确使用来保证跨平台
- ASCII码字符表
- ASCII码字符表
- ASCII 码字符
- ASCII码字符表
- 使用RabbitMQ传输大文件,保证其完整性
- 将整型的ASCii码值转换为对应的ASCii码字符串
- ASCII码字库的显示
- C#中16进制转化成ASCII码字符
- 3章7节使用ASCII码字符
- ASCII码字符加密解密
- ASCII码字符对照表
- ASCII码字符对照表
- ASCII码字符对照表
- 学习nodejs第一课,关于js的闭包问题。
- 机器人操作系统ROS Indigo 入门学习(0)——ROS的UNIX基础
- 在自己的网站上实现QQ授权登录
- 大型网站架构系列:缓存在分布式系统中的应用(一)
- Java基础: 位运算 << >> >>>
- 跨平台传输中使用base64来保证非ascii码字符串的完整性
- 如何让 Spring Boot 项目发布到 Tomcat 服务器
- 【git】看懂git diff
- 注意了!你的工资将决定这个冬天你的御寒方式!
- adb logcat 查看日志命令行用法
- 怎样才能将Excel中隐藏的列显示出来
- UVa 10017 - The Never Ending Towers of Hanoi
- ubuntu出现有线已连接却无法上网的解决方法(ubuntu连不上网)
- 菜鸟之--Python的一些使用