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

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

转载于:https://www.cnblogs.com/davidwang456/p/3935601.html

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

  1. RFC#2457——Rust 语言支持非 ASCII 码标识符在 GitHub 引发的激辩(一)

    Everything from Python to C++ supports non-ASCII idents by default. It's the correct behaviour. -- G ...

  2. RFC#2457——Rust 语言选择支持非 ASCII 码标识符在 GitHub 引发的激辩(二)

    难难难,道德玄,不对知音不可谈.对了知音谈几句,不对知音枉费舌尖! 书接上文,至 2018 年 6 月 5 日,RFC#2457刚创建三天,已看到了不少反对声.在非英语母语的参与者中,华人开发者群体尤 ...

  3. HTML实体字符、ASCII码、URLEncoder、Base64、MD5

    HTML 字符实体 在 HTML 中,某些字符是预留的. 在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签. 如果希望正确地显示预留字符,我们必须在 H ...

  4. ASCII码 和 Base64编码

    ASCII编码 简介 ASCII码(American Standard Code for Information Interchange, 美国信息互换标准代码) 是基于拉丁字母的一套电脑编码系统, ...

  5. ASP.NET 或C# 中ASCII码含中文字符的编解码处理

    网上很多在线工具转出来的不能用,如"是是是"转出来的ASCII码是 :"\u662f\u662f\u662f",如果是HTTP服务这个参数是有问题的因为包含特殊 ...

  6. 网络传输中的两个阶段、阻塞IO、非阻塞IO和多路复用

    今天学习了网络传输中的两个阶段.阻塞IO.非阻塞IO和多路复用 一.网络传输中的两个阶段 分别是 waitdata 和 copydata send就是copydata recv是waitdata和co ...

  7. 网络传输中的那些编码之-base64编码

    在前面一篇文章中,这里介绍了URL编码的一些知识点,本文将对网络中另外一个常见的编码方式base64编码进行介绍. 同样的,按照之前的介绍方式,本文将从如下几个方面进行介绍: 什么是base64编码 ...

  8. 实时视频传输中的BBR拥塞控制

    在复杂的网络环境中,想要实现实时视频传输,拥塞控制算法是尤为重点的一环.本文整理自学霸君高级技术总监袁荣喜在LiveVideoStackCon 2019上海大会中的分享,详细介绍了BBR拥塞控制算法在 ...

  9. 数据链路层 功能 封装成帧 透明传输 字符计数法 字符填充法 零比特填充法 违规编码法 传输中的差错 差错控制 冗余编码 奇偶校验码 CRC循环冗余码 检错过程 细解 图解 通俗易懂

    粉丝不过W 数据链路层: 结点:主机.路由器 链路:网络中两个结点之间的物理通道,链路的传输介质:双绞线.光纤和微波,分为有线链路.无线链路 数据链路:网络中两个结点之间的逻辑通道,把实现控制数据传输 ...

最新文章

  1. 数据挖掘技术在出行体验上的应用!
  2. 数据显示,近半数人依然坚持在用 Windows 7
  3. 按钮美化,变化显示效果
  4. (2)双机调试+符号文件
  5. 为了你,我一定要写诗
  6. 【渝粤教育】电大中专电商运营实操 (25)作业 题库
  7. 从SQL过渡至MongoDB查询对照表
  8. ssh 连接超时 不断开
  9. 全彩控制器的编程软件有哪些_可编程LED控制器-MINI全彩控制器软件(DC-Color)v1.08 官方版-腾牛下载...
  10. uefi 懒人版黑苹果_clover+懒人版黑苹果安装(e3+970)
  11. 微信收款没有提示通知消息,怎么打开?解决方案
  12. 【Spinning up】零、DRLib:一个简洁的强化学习库,集成了HER和PER
  13. 在苹果Mac上怎样对“屏幕使用时间”中请求更多时间进行响应?
  14. vue中echarts使用案例:饼图(可直接使用)
  15. 亿道丨三防平板丨加固平板丨三防工业平板丨航空航天应用
  16. 关于mp4格式转m3u8切片加密的方案调研
  17. 抽象方法和抽象类规则
  18. inline 成员函数
  19. 2021-2027全球与中国汽车CMOS图像传感器市场现状及未来发展趋势
  20. 水压计算机控制系统原理图,水压试验机控制系统研究与开发

热门文章

  1. 本科985末端去哪学计算机好,4所“985高校”,录取分较低,常被拿来捡漏!
  2. win10 搭载文件服务器,win10远程文件服务器
  3. java concurrent 框架,java.util.concurrent 包下的 Synchronizer 框架
  4. 类型两个数相减_小学数学简便计算12种分类+5种易错类型,打印出来给孩子练习!(可打印!)...
  5. C++基本序列式容器效率比较
  6. Python 位运算符号
  7. 论文笔记:Spatial-Temporal Map Vehicle Trajectory Detection Using Dynamic Mode Decomposition and Res-UNe
  8. Flink从入门到精通100篇(二十四)-对Flink SQL Client 源码做深度解析
  9. R语言实战应用精讲50篇(三十一)-R语言入门系列-tidyverse数据分析流程
  10. 深度学习核心技术精讲100篇(八十一)-NLP预训练模型ERNIE实战应用案例