如何将带有UTF-8代码的字符串转换为java中的纯文本 [英] How do I convert a string with a UTF-8 code to plain text in java

查看:116
本文介绍了如何将带有UTF-8代码的字符串转换为java中的纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含C3A1的字符串,它是utf-8相当于á

如何在Java中将其转换为á



我尝试了什么:



我已经尝试了一些方法来解析它作为一个字符但没有任何工作

I have a string which contains "C3A1" which is the utf-8 equivalent of á
How do I convert this to á in Java.

What I have tried:

I've tried some ways to parse it as a char but nothing's worked

推荐答案

如果你有一个表示UTF-8字符串的字节数组,请使用接受字节数组的Java String 构造函数编码:

If you have a byte array representing an UTF-8 string use the Java String constructor accepting a byte array and an encoding:
String str = new String(utf8Bytes, "UTF-8");

如果您确实有一个包含UTF-8字符串的 String 对象,则必须先将其转换为字节数组:

If you really have a String object containing an UTF-8 string, you have to convert it first to a byte array:

byte[] utf8Bytes = inputStr.getBytes("ISO-8859-1");

您可以在上面使用任何完整的8位编码。它仅用于确保输入字符串中的每个字节都被复制到字节数组。

You can use any full 8-bit encoding in the above. It is just used to ensure that each single byte from the input string is copied to the byte array.


Quote:

如何在Java中将其转换为á。

How do I convert this to á in Java.



按原样,这是没有意义的。

因为'á'不是ASCII,你需要了解一些事情:

- 什么是ascii代码

- 什么是带有代码页的ascii

- 什么是UTF编码和UTF8

- 您需要了解Java如何处理字符编码。



然后您将决定在代码中使用的编码。取决于编码,字符不一样。


As is, this is non sense.
As 'á' is not ASCII, you need to understand a few things:
- what is ascii code
- what is ascii with codepages
- what is UTF encoding and UTF8
- you need to understand how Java handle char encoding.

Then you will decide what encoding you will use in your code. depending on encoding, the char is not the same.

引用:

我尝试过一些方法来解析它作为一个焦点,但没有任何工作

I've tried some ways to parse it as a char but nothing's worked



你只是没有显示你做了什么。


You only haven't show what you have done.


这篇关于如何将带有UTF-8代码的字符串转换为java中的纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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