前一段日子,我在做rsa加密和通过http get方式获取验证码图片通过BitmapFactory创建bitmap

出现了一系列的问题。

通过一系列的调试,发现有些问题原来是在进行String 与Byte[]之间转换造成的。

哎,android新手伤不起啊。。。。。

java 默认String在内存中的编码是ucs-2编码。当你要把byte[]转换成String时,这里就涉及到了

编码转换的问题,假如你不指定byte[]里面的编码,那可能在转换后会有问题。假如你没有指定

byte[]里面所用到的编码,转换就会根据当前系统环境给你指定一个编码,在android系统中就会默认byte[]中的数据是

用utf8编码的。在android中如果byte[]中的数据不是utf8编码,那么使用默认方式转换到String时,这时就造成转换后

的数据出现了问题,再将出问题的String转换回byte[]时,同样会有问题。

所以在获取一些不知道编码的二进制数据的时候,不要转换成String,在android中使用byte[],或者InputStream

来存储,传输,处理二进制数据就行。

java String与Byte[]和String 与InputStream转换时注意编码问题。。。的更多相关文章

  1. java图片转byte转string

    第一种:原始乱码: public static void main(String[] args) throws IOException { File imgFile = new File(" ...

  2. Java 把 InputStream 转换成 String 的几种方法

    我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之 ...

  3. Java 里把 InputStream 转换成 String 的几种方法

    我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之 ...

  4. java的byte[]与String相互转换

    String转byte[] byte[] sInput = new byte[0]; try { // 可以指定编码,默认也只UTF-8 sInput = "这是内容".getBy ...

  5. C# Byte[] 转String 无损转换

    C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...

  6. [转]关于网络通信,byte[]和String的转换问题

    最近的项目中要使用到把byte[]类型转换成String字符串然后通过网络发送,但发现发现出去的字符串和获取的字符串虽然是一样的,但当用String的getBytes()的方法得到的byte[]跟原来 ...

  7. golang string和[]byte的对比

    golang string和[]byte的对比 为啥string和[]byte类型转换需要一定的代价?为啥内置函数copy会有一种特殊情况copy(dst []byte, src string) in ...

  8. dotnet中Stream、string及byte[]的相关操作

    string与byte[](UTF-8) //string to byte[] string str = "abc中文"; //0x61 0x62 0x63 0xE4 0xB8 0 ...

  9. byte ---> hex String

    public static String byte2HexString(byte[] b){ String ret = ""; ;i<b.lenght;i++){ Strin ...

随机推荐

  1. 一次编译Android源码实验

    注意,本文只供参考,是老文章 1.必要的软件环境 sudo apt-get install build-essential sudo apt-get install make sudo apt-get ...

  2. Linux阵列 RAID详解

    主要内容:(笔记总结--会不定时补充.修改,如有错误,欢迎指正探讨)    一. RAID详解   二. mdadm工具介绍   三. 创建一个RAID的基本过程   四. 磁盘阵列的管理   五. ...

  3. BQ27510 电量计的校准 的 C语言实现

    点击打开链接 根据TI官方MSP430平台移植修改过来的,在Omap37xx(wince)平台测试,理论上和平台无关,伸手党,赶紧复制粘贴代码吧.如果这篇文章帮助了你,给了好评也无妨. [cpp] v ...

  4. nuget.exe the application could not be started

    http://stackoverflow.com/questions/5730412/error-when-running-the-nuget-exe-command Ok, so this turn ...

  5. Creating Excel File in Oracle Forms

    Below is the example to create an excel file in Oracle Forms.Pass the Sql query string to the below ...

  6. Django基础-过滤器

    1.可以通过过滤器来修改变量的显示,过滤器的形式是:{{ variable | filter }},管道符号'|'代表使用过滤器 2.过滤器能够采用链式的方式使用,例如:{{ text | escap ...

  7. windows c dll的创建与调用

    DLL代码: // TestDynamic.cpp: implementation of the TestDynamic class. // ///////////////////////////// ...

  8. Shared Assembilies and Strongly Named Assemblies

    the .NET Framework has in place to deal with versioning problems. Two Kinds of Assemblies, Two Kinds ...

  9. javascript权威指南笔记--javascript语言核心(五)--getter和setter属性

    getter和setter属性: var p = { x:1.0, y:1.0, get r(){ return Math.sqrt(this.x*this.x + this.y * this.y); ...

  10. CSS笔记(七)列表

    CSS 列表属性允许你放置.改变列表项标志,或者将图像作为列表项标志. 参考:http://www.w3school.com.cn/css/css_list.asp 实例: <html> ...