public class EncodingUtil {
// 这里可以提供更多地编码格式,另外由于部分编码格式是一致的所以会返回 第一个匹配的编码格式 GBK 和 GB2312
public static final String[] encodes = new String[] { "UTF-8", "GBK", "GB2312", "ISO-8859-1", "ISO-8859-2" }; /**
* 获取字符串编码格式
*
* @param str
* @return
*/
public static String getEncode(String str) {
byte[] data = str.getBytes();
byte[] b = null;
a:for (int i = ; i < encodes.length; i++) {
try {
b = str.getBytes(encodes[i]);
if (b.length!=data.length)
continue;
for (int j = ; j < b.length; j++) {
if (b[j] != data[j]) {
continue a;
}
}
return encodes[i];
} catch (UnsupportedEncodingException e) {
continue;
}
}
return null;
} /**
* 将字符串转换成指定编码格式
*
* @param str
* @param encode
* @return
*/
public static String transcoding(String str, String encode) {
String df = "ISO-8859-1";
try {
String en = getEncode(str);
if (en == null)
en = df;
return new String(str.getBytes(en), encode);
} catch (UnsupportedEncodingException e) {
return null;
}
}
}

java获取字符串编码和转换字符串编码的更多相关文章

  1. Java获取任意时间、时间字符串

    /* * 获取时间字符串*/public String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  2. Java获取当前时间戳/时间戳转换

    时间戳精度有两个概念:1是精确到秒,2是精确到毫秒. 要操作时间戳和时间戳转换为时间一般对应的对象就是Date,而Date各种转换离不开SimpleDateFormat: 如果是要获取时间指定的年月日 ...

  3. mac 查看文件编码及转换文件编码

    方法是用vim , vim 打开相应文件, :set fileencoding即可显示文件编码格式 将文件编码转换为utf-8 :set fileencoding=utf-8

  4. java转换字符串的编码(转)

    package com.Alex.base; import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public class C ...

  5. java安全编码指南之:字符串和编码

    目录 简介 使用变长编码的不完全字符来创建字符串 char不能表示所有的Unicode 注意Locale的使用 文件读写中的编码格式 不要将非字符数据编码为字符串 简介 字符串是我们日常编码过程中使用 ...

  6. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  7. 在Linux下使用iconv转换字符串编码

    在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...

  8. PHP 字符串编码的转换

    原文链接:http://mangguo.org/php-string-encoding-convert-and-detect/ GBK 和 UTF-8 编码的转换是一个非常恶心的事情,比如像 PHP ...

  9. 使用Word 进行UTF8 以及字符串编码的转换操作

    1. 使用Word文档能够实现 字符串和utf8编码的转换. 快捷键是 ALT+X 在知乎的一个里面看到一个说法: ㍾ ㍽ ㍼ ㍻ - 这四个在Unicode表里是倒序排列的,而且只预留了这四个年号, ...

随机推荐

  1. Intel CPUs

    http://en.wikipedia.org/wiki/Intel_cpus List of Intel Atom microprocessors List of Intel Xeon microp ...

  2. 哪位有方法把 dd/mm/yyyy的字符串 格式化成yyyy-mm-dd

     哪位有方法把  dd/mm/yyyy的字符串 格式化成yyyy-mm-dd[总监]Dawood(656317124)  10:00:42啊,找到方法了.procedure TForm1.Button ...

  3. php如何获取服务器端的一些信息

    <?php echo "服务器端的IP地址是:<br />"; echo $_SERVER['SERVER_ADDR']; echo "服务器端的端口号 ...

  4. HDU 1028 Ignatius and the Princess III (动态规划)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

  5. js比较日期时间的大小

    var myDate = new Date(); var timed = myDate.toLocaleDateString(); var oDate1 = new Date(item.express ...

  6. Java并发AtomicIntegerArray类

    java.util.concurrent.atomic.AtomicIntegerArray类提供了可以以原子方式读取和写入的底层int数组的操作,还包含高级原子操作. AtomicIntegerAr ...

  7. sublime text3中使用PHP编译系统

    前言: php是服务器端语言,我们平时写的php代码想要查看运行结果的话,通常会搭建web服务器,然后通过浏览器访问.而对于有时候一些简单的测试代码来说,此过程就有点繁琐了.编译系统的好处是,可以让我 ...

  8. Skimap_ros 利用RGBD创建Octomap(一)

    1. 奥比中光astra RGBD相机安装 1.1 安装依赖 $ sudo apt-get install build-essential freeglut3 freeglut3-dev 1.2 检查 ...

  9. Python3学习笔记——自定义模块

    import sys import os print(__file__) #打印相对路径 base_dir = os.path.dirname(os.path.dirname(os.path.absp ...

  10. 面试题:实现call、apply、bind

    面试题:实现call.apply.bind 实现bind module.exports = function(Tcontext, ...args) { let globalThis = typeof ...