[j2me]如何获取字符编码如何将编码转为字符

[j2me]如何获取字符编码如何将编码转为字符,第1张

看这能否帮助你。

import javaioByteArrayOutputStream;

import javaioUTFDataFormatException;

import javautilVector;

public class Utils {

public static String byteArrayToString(byte[] bytes, String encoding) {

char[] map = "\u0402\u0403\u201A\u0453\u201E\u2026\u2020\u2021\u20AC\u2030\u0409\u2039\u040A\u040C\u040B\u040F\u0452\u2018\u2019\u201C\u201D\u2022\u2013\u2014\uFFFD\u2122\u0459\u203A\u045A\u045C\u045B\u045F\u00A0\u040E\u045E\u0408\u00A4\u0490\u00A6\u00A7\u0401\u00A9\u0404\u00AB\u00AC\u00AD\u00AE\u0407\u00B0\u00B1\u0406\u0456\u0491\u00B5\u00B6\u00B7\u0451\u2116\u0454\u00BB\u0458\u0405\u0455\u0457\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041A\u041B\u041C\u041D\u041E\u041F\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042A\u042B\u042C\u042D\u042E\u042F\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043A\u043B\u043C\u043D\u043E\u043F\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044A\u044B\u044C\u044D\u044E\u044F"toCharArray();

if (encodingequals("UTF-8")) {

encoding = "ISO-8859-1";

}

try {

return decodeUTF8(bytes, false);

} catch(UTFDataFormatException udfe) {}

char[] chars = new char[byteslength];

for (int i = 0; i < byteslength; i ) {

byte b = bytes[i];

chars[i] = (b >= 0) (char) b : map[b 128];

}

return new String(chars);

}

public static byte[] encodeUTF8(String text) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] ret;

for (int i = 0; i < textlength(); i ) {

char c = textcharAt(i);

if (c != '\u0000' && c < '\u0080') {

baoswrite(c);

} else if (c == '\u0000' || (c >= '\u0080' && c < '\u0800')) {

baoswrite((byte)(0xc0 | (0x1f & (c >> 6))));

baoswrite((byte)(0x80 | (0x3f & c)));

} else {

baoswrite((byte)(0xe0 | (0x0f & (c >> 12))));

baoswrite((byte)(0x80 | (0x3f & (c >> 6))));

baoswrite((byte)(0x80 | (0x3f & c)));

}

}

ret = baostoByteArray();

return ret;

}

private static String decodeUTF8(byte[] data, boolean gracious) throws UTFDataFormatException {

byte a,

b,

c;

StringBuffer ret = new StringBuffer();

for (int i = 0; i < datalength; i ) {

try {

a = data[i];

if ((a & 0x80) == 0) {

retappend((char) a);

} else if ((a & 0xe0) == 0xc0) {

b = data[i 1];

if ((b & 0xc0) == 0x80) {

retappend((char)(((a & 0x1F) << 6) | (b & 0x3F)));

i ;

} else {

throw new UTFDataFormatException("Illegal 2-byte group");

}

} else if ((a & 0xf0) == 0xe0) {

b = data[i 1];

c = data[i 2];

if (((b & 0xc0) == 0x80) && ((c & 0xc0) == 0x80)) {

retappend((char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)));

i = 2;

} else {

throw new UTFDataFormatException("Illegal 3-byte group");

}

} else if (((a & 0xf0) == 0xf0) || ((a & 0xc0) == 0x80)) {

throw new UTFDataFormatException("Illegal first byte of a group");

}

} catch(UTFDataFormatException udfe) {

if (gracious) {

retappend("");

} else {

throw udfe;

}

} catch(ArrayIndexOutOfBoundsException aioobe) {

if (gracious) {

retappend("");

} else {

throw new UTFDataFormatException("Unexpected EOF");

}

}

}

return rettoString();

}

public static String[] splitString(String str, String delim) {

if (str == null) {

return null;

} else if (strequals("") || delim == null || delimlength() == 0) {

return new String[] {

str

};

}

String[] s;

Vector v = new Vector();

int pos,

newpos;

pos = 0;

newpos = strindexOf(delim, pos);

while (newpos != -1) {

vaddElement(strsubstring(pos, newpos));

pos = newpos delimlength();

newpos = strindexOf(delim, pos);

}

vaddElement(strsubstring(pos));

s = new String[vsize()];

for (int i = 0; i < slength; i ) {

s[i] = (String) velementAt(i);

}

return s;

}

}

可以通过以下方法来进行编码格式判断,输入一个字符串,之后返回字符串编码类型。

public static String getEncoding(String str) {

String encode = "GB2312";

try {

if (strequals(new String(strgetBytes(encode), encode))) { //判断是不是GB2312

String s = encode;

return s; //是的话,返回“GB2312“,以下代码同理

}

} catch (Exception exception) {

}

encode = "ISO-8859-1";

try {

if (strequals(new String(strgetBytes(encode), encode))) { //判断是不是ISO-8859-1

String s1 = encode;

return s1;

}

} catch (Exception exception1) {

}

encode = "UTF-8";

try {

if (strequals(new String(strgetBytes(encode), encode))) { //判断是不是UTF-8

String s2 = encode;

return s2;

}

} catch (Exception exception2) {

}

encode = "GBK";

try {

if (strequals(new String(strgetBytes(encode), encode))) { //判断是不是GBK

String s3 = encode;

return s3;

}

} catch (Exception exception3) {

}

return ""; //如果都不是,说明输入的内容不属于常见的编码格式。

byte[] b=stringgetBytes("GB2312");//使用GB2312编码方式对字符串string进行编码

//这时要想将字节数组b的内容正确解码只能使用GB2312的编码方式进行解码,即

String str=new String(b,"GB2312");//这里若使用UTF-8编码方式来进行解码就会乱码

//将eclipse默认的编码方式改为UTF-8,只是用该编码方式对java源文件进行编码保存

//这个对new String(stringgetBytes("GB2312"),"UTF-8")没啥影响的

//因为从java源文件获取字符串string时,已经通过UTF-8编码方式进行解码了

//而stringgetBytes("GB2312")是使用指定的编码方式对字符串string进行从新编码

//这两者之间没啥关系的

这样的测试方法是不正确的。getBytes(charset)是解码,new String(byte[], charset)是编码。new String(strgetBytes(charset),charset)是解码再编码,无论charset是什么编码格式,返回的字符串内容原始str是一致,因此equals方法都是返回true,达不到测试字符串编码格式的目的。个人观点:正确的测试姿势应该是这样的:

String charset ="xxx"; //假定编码格式

String str = "中文";

boolean flag = strequals(new String(strgetBytes(),charset));

flag为true则表明str的编码格式是假定的编码格式。其中说明strgetBytes()该方法就是按其自身编码格式去解码。其自身编码格式跟你的 *** 作系统编码格式或你使用的IDE设置的文件的Text file encoding有关。

以上就是关于[j2me]如何获取字符编码如何将编码转为字符全部的内容,包括:[j2me]如何获取字符编码如何将编码转为字符、如何查看字符编码类型、请问java如何改变字符串的编码方式等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9552799.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存