猜测法:猜测一种字符串编码,然后使用该编码对字符串进行编码,还原。如果猜测错误,字符串会被破坏,还原城乱码。
/**
* 判断字符串编码
*
* @param str
* @return
*/
public static String getEncoding (String str)
{
String encode = "GB2312";
try
{
if (str.equals (new String (str.getBytes (encode), encode)))
{
String s = encode;
return s;
}
}
catch (Exception exception)
{
}
encode = "ISO-8859-1";
try
{
if (str.equals (new String (str.getBytes (encode), encode)))
{
String s1 = encode;
return s1;
}
}
catch (Exception exception1)
{
}
encode = "UTF-8";
try
{
if (str.equals (new String (str.getBytes (encode), encode)))
{
String s2 = encode;
return s2;
}
}
catch (Exception exception2)
{
}
encode = "GBK";
try
{
if (str.equals (new String (str.getBytes (encode), encode)))
{
String s3 = encode;
return s3;
}
}
catch (Exception exception3)
{
}
return "";
}
分享到:
相关推荐
基于C#语言,可自行改为c即可用于c++ 一个判断字符串编码类型的类,支持多种常见编码
介绍了几种判断字符串编码方法 1、利用正则来判断 2、使用iconv库来判断 3、chardet库来判断
判断字符串编码是utf-8还是gb23121
判断字符串编码是utf-8还是gb2312 一、 开发环境 1、环境搭建:Windows 7+Apache 2.4.18+MySQL 5.7.11+PHP 7.1.0。 2、文本编辑器:Sublime 3。 二、主要技术 本案例主要使用PHP通过对字符的ASCII码值进行判断,从而...
本文实例讲述了C#简单判断字符编码的方法。分享给大家供大家参考,具体如下: public static string GetText(byte[] buff) { string strReslut = string.Empty; if (buff.Length > 3) { if (buff[0] == 239 && ...
下面是一个判断字符串是否为utf8的函数: bool is_str_utf8(const char* str) { unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节 unsigned char chr = *str; bool bAllAscii = t
本文实例讲述了python判断字符串编码的方法。分享给大家供大家参考,具体如下: 安装chardet模块 chardet文件夹放在/usr/lib/python2.4/site-packages目录下 [root@sha-sso-data01 chardet]# python Python 2.4.3 ...
判断字符编码格式代码,用C++编写的如何判断字符编码格式,UTF-8,ASCII等
php 判断字符串编码是utf-8 或gb2312 第一种方法: function is_gb2312($str) { for($i=0; $i<strlen xss=removed> 127) { if( ($v >= 228) && ($v <= 233) ) { if( ($i+2) >= (strlen($str) - 1)) return ...
不需要关心接受的字符串编码是UTF_8还是GBK,还是ios-8859-1,自动转换为utf-8编码格式,无需判断字符串原有编码,用法://处理编码String newStr = GetEncode.transcode(oldStr);
本文实例讲述了python使用chardet判断字符串编码的方法。分享给大家供大家参考。具体分析如下: 最近利用python抓取一些网上的数据,遇到了编码的问题。非常头痛,总结一下用到的解决方案。 linux中vim下查看文件...
判断字符串是否包含emoji表情
qt实现转换文件字符集编码
主要介绍了C#判断字符编码的方法,结合实例形式总结分析了六种C#判断字符编码的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
主要介绍了Android判断字符串中是否含字母、中文或数字的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下