首先,我们来看一个例子:

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

Methods 
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等。

跨平台传输中使用base64来保证非ascii码字符串的完整性的更多相关文章

  1. Python3选择支持非ASCII码标识符的缘由

    原文在: PEP 3131 -- Supporting Non-ASCII Identifiers. Python2并不支持非ASCII码标识符. PEP的全称是Python Enhancement ...

  2. NET MVC全局异常处理(一) 【转载】网站遭遇DDoS攻击怎么办 使用 HttpRequester 更方便的发起 HTTP 请求 C#文件流。 Url的Base64编码以及解码 C#计算字符串长度,汉字算两个字符 2019周笔记(2.18-2.23) Mysql语句中当前时间不能直接使用C#中的Date.Now传输 Mysql中Count函数的正确使用

    NET MVC全局异常处理(一)   目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关 ...

  3. Python中的Base64编码的加密与解密

    Base64 可以干些啥? Base64编码的作用: 由于某些系统中只能使用ASCII字符.Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法. 图片(and种子)base64 ...

  4. Python中进行Base64编码和解码

    Base64编码 广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容 ...

  5. java学习-http中get请求的非ascii参数如何编码解码探讨

    # 背景: 看着别人项目代码看到一个PathUtils工具类, 里面只有一个方法,String  rebuild(String Path),将路径进行URLDecoder.decode解码,避免路径中 ...

  6. python中的base64加密解密

    介绍 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法.可查看RFC2045-RFC2049,上面有MIME的详细规范. ...

  7. [转载]详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表

    [转载]详解网络传输中的三张表,MAC地址表.ARP缓存表以及路由表 虽然学过了计算机网络,但是这部分还是有点乱.正好在网上看到了一篇文章,讲的很透彻,转载过来康康. 本文出自 "邓奇的Bl ...

  8. 在python中实现BASE64编码

    什么是Base64编码 BASE64是用于传输8Bit字节的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法. 如下是转换表:The Base64 Alphabet Base64编码可以 ...

  9. netty系列之:java中的base64编码器

    简介 什么是Base64编码呢?在回答这个问题之前,我们需要了解一下计算机中文件的分类,对于计算机来说文件可以分为两类,一类是文本文件,一类是二进制文件. 对于二进制文件来说,其内容是用二进制来表示的 ...

随机推荐

  1. Protocol Buffer Xcode 正确使用思路 成功安装 Xcode7.1

    1. 下载protobuf编译工具 序列化是将数据转换为一个特定的类 http://pan.baidu.com/s/1qWrxHxU 下载解压,它不是用来放在你的项目里 2.打开终端 依次输入并等待指 ...

  2. ASP.NET菜鸟之路之Request小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Request获取值 Request获取值有两种 ...

  3. JavaScript的“闭包”到底是什么(2)

    我的上篇博客标题不对,造成一些误解.我认为博客的宗旨不是背教科书,而是分享研发心得.我的上篇标题因该改成“JavaScript 闭包的一个议题:它对outer scope 的影响”,因为我没有严格地去 ...

  4. 逆向iOS SDK -- _UIImageAtPath 的实现(SDK 5.1)

    注释过的反汇编代码:http://pan.baidu.com/share/link?shareid=3491166579&uk=537224442 伪代码(不精确,仅供参考): NSStrin ...

  5. CMake----if与option使用小记

    在CMake中if语法比较简单,if后面括号中的参数随着CMake版本的推进,在else和endif中也可以不用写了. if(address) else() endif() 对于if语法,比较常用的就 ...

  6. 导出页面文档(只在IE8下测试过)

    之前说过一篇关于打印的方法,就顺便也看了一下导出,但是该方法需要用户更改浏览器的安全级别设置,因此并不十分推荐,大家如真有需要可以参考一下ZeroClipboard这款插件,我有时间也会去学习一下并贴 ...

  7. html5 input属性

    今天才接确html5 +css3 实在是太赞了.  下面我就来介绍一下今天我用到的 input 属性. html5 代码如下: <input type="text" plac ...

  8. ppt怎么换背景图片|PPT换背景设置方法

    PPT怎么换背景?PPT背景可谓是PPT幻灯片的灵魂,优美绚丽的PPT背景能为自己做的PPT幻灯片锦上添花.今天,格子啦小编就教大家PPT换背景的方法,让你不再羡慕别人所做PPT的美丽背景,也可以自己 ...

  9. 转:嵌入式linux启动时运行的inittab文件

    嵌入式系统下的linux启动配置文件,不同与普通的PC linux启动配置,启动相关文件与文件的内容也要少得多.嵌入式系统下的linux启动过程一般是:     1 在bootloader中制定各种要 ...

  10. Java中start和run方法的区别

    一.问题引入         说到这两个方法就不得不说多线程,说到多线程就不得不提实现多线程的两种方式继承Thread类和实现Runable接口,下面先看这两种方式的区别. 二. Java中实现多线程 ...