将字节数组转换为字符串(Java) [英] Converting byte array to String (Java)

查看:267
本文介绍了将字节数组转换为字符串(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Google App Engine 中编写一个网络应用程序.它允许人们基本上编辑作为 .html 文件存储在 blobstore 中的 html 代码.

I'm writing a web application in Google app Engine. It allows people to basically edit html code that gets stored as an .html file in the blobstore.

我正在使用 fetchData 返回文件中所有字符的 byte[].我正在尝试打印到 html 以便用户编辑 html 代码.一切都很好!

I'm using fetchData to return a byte[] of all the characters in the file. I'm trying to print to an html in order for the user to edit the html code. Everything works great!

这是我现在唯一的问题:

Here's my only problem now:

字节数组在转换回字符串时存在一些问题.聪明的引语和几个字符看起来很时髦.(?或日语符号等)具体来说,我看到有几个字节具有导致问题的负值.

The byte array is having some issues when converting back to a string. Smart quotes and a couple of characters are coming out looking funky. (?'s or japanese symbols etc.) Specifically it's several bytes I'm seeing that have negative values which are causing the problem.

智能引号在字节数组中以 -108-109 的形式返回.这是为什么,我如何解码负字节以显示正确的字符编码?

The smart quotes are coming back as -108 and -109 in the byte array. Why is this and how can I decode the negative bytes to show the correct character encoding?

推荐答案

字节数组包含特殊编码的字符(您应该知道).将其转换为字符串的方法是:

The byte array contains characters in a special encoding (that you should know). The way to convert it to a String is:

String decoded = new String(bytes, "UTF-8");  // example for one encoding type

顺便说一句 - 原始字节可能显示为负小数,因为 java 数据类型 byte 是有符号的,它涵盖了 -128 到 127 的范围.

By The Way - the raw bytes appear may appear as negative decimals just because the java datatype byte is signed, it covers the range from -128 to 127.

-109 = 0x93: Control Code "Set Transmit State"

值 (-109) 在 UNICODE 中是不可打印的控制字符.因此 UTF-8 不是该字符流的正确编码.

The value (-109) is a non-printable control character in UNICODE. So UTF-8 is not the correct encoding for that character stream.

0x93 是您要查找的智能引用",因此该编码的 Java 名称为Cp1252".下一行提供了一个测试代码:

0x93 in "Windows-1252" is the "smart quote" that you're looking for, so the Java name of that encoding is "Cp1252". The next line provides a test code:

System.out.println(new String(new byte[]{-109}, "Cp1252")); 

这篇关于将字节数组转换为字符串(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆