public class DataUtil {

    public static void main(String[] args) {
int a = 8;
int value = charToInt(byteToChar(intToByte(a)));
int value2 = byteToInt(charToByte(intTochar(a)));
System.out.println(value);
System.out.println(value2);
} public static byte[] intToByte(int value){
byte[] b = new byte[4];
b[0] = (byte) (value & 0xff);
b[1] = (byte) ((value >> 8) & 0xff);
b[2] = (byte) ((value >> 16) & 0xff);
b[3] = (byte) ((value >> 24) & 0xff);
return b;
} public static int byteToInt(byte[] bytes){
return (bytes[0] & 0xff) | ((bytes[1] & 0xff) << 8)
| ((bytes[2] & 0xff) << 16) | ((bytes[3] & 0xff) << 24);
} public static char[] intTochar(int value){
char[] c = new char[2];
c[0] = (char) (value & 0xffff);
c[1] = (char) ((value >> 16) & 0xffff);
return c;
} public static int charToInt(char[] chars){
return (chars[0] & 0xffff) | ((chars[1] & 0xffff) << 16);
} public static char[] byteToChar(byte[] bytes){
char[] v = new char[2];
v[0] = (char) ((bytes[0] & 0xffff) | ((bytes[1] & 0xffff) << 8));
v[1] = (char) ((bytes[2] & 0xffff) | ((bytes[3] & 0xffff) << 8));
return v;
} public static byte[] charToByte(char[] chars){
byte[] bytes = new byte[4];
bytes[0] = (byte) (chars[0] & 0xff);
bytes[1] = (byte) ((chars[0] >> 8) & 0xff);
bytes[2] = (byte) (chars[1] & 0xff);
bytes[3] = (byte) ((chars[1] >> 8) & 0xff);
return bytes;
} public static byte[] objectToBytes(Object object) throws IOException{
try(ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(arrayOutputStream)){
outputStream.writeObject(object);
outputStream.flush();
return arrayOutputStream.toByteArray();
}
} public static Object bytesToObject(byte[] bytes) throws IOException,ClassNotFoundException{
try (ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(arrayInputStream)){
return objectInputStream.readObject();
}
}
}

基本类型数据转换(int,char,byte)的更多相关文章

  1. 将COleDateTime类型数据转换成char *数据

    用OpenCV做多摄像头校准时间,在图像上显示时间信息,需求要将COleDateTime类型数据转换成char *数据 具体代码如下: 1: COleDateTime m_checkDate; 2: ...

  2. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  3. (C语言)char类型与int类型相加

    #include <stdio.h> int main(void) { ; ; int c = a + b; a += b; printf("c=%d",c); //p ...

  4. java基础类型中的char和byte的辨析及Unicode编码和UTF-8的区别

    在平常工作中使用到char和byte的场景不多,但是如果项目中使用到IO流操作时,则必定会涉及到这两个类型,下面让我们一起来回顾一下这两个类型吧. char和byte的对比 byte byte 字节, ...

  5. java笔记--关于int和byte[]的转换

    关于int和byte[]数组的转换 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3891747.html "谢谢-- 众所 ...

  6. int与byte的区别

    Java中涉及byte.short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果.因此,如果把两个byte类型值相加,最后会得到一个int ...

  7. mysql java 类型对照 int

    Java MySQL数据类型对照 类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+N VARCHAR java.lang.S ...

  8. java int转byte和long转byte

    在网络编程中,出于节约带宽或者编码的需要,通常需要以原生方式处理long和int,而不是转换为string. public class ByteOrderUtils { public static b ...

  9. C# int转byte[],byte[]转int

    第一种方法: byte数组转int u = (uint)(b[0] | b[1] << 8 |b[2] << 16 | b[3] << 24); int转byte数 ...

随机推荐

  1. 通过GIT_COMMIT进行代码回滚

    首先需要安装插件:conditional-buildstep A buildstep wrapping any number of other buildsteps, controlling thei ...

  2. JS & JQuery 动态处理select option

    原文 出处http://www.51xuediannao.com/html+css/htmlcssjq/cssbuhuanhang.html 今天你问了我一个关于在<select>里动态添 ...

  3. Spring Boot 整合Quartz定时器

    概述 项目需要定时器的调度管理,原来使用Spring Boot自带的定时器,但是不能后台动态的操作暂停.启动以及新增任务等操作,维护起来相对麻烦:最近研究了Quartz的框架,觉得还算不错,整理了一下 ...

  4. mysql 和 sqlserver sql差异比较

    mysql:select * from table_name limit 100,200;--取出从100到200的数据 获取时间:mysql:now() mysql tinyint(0,1) → b ...

  5. vim 使用学习操作

    1 跳转 命令 作用 h 光标向左移动 l 光标向右移动 j 光标向上移动 k 光标向下移动 w 移动光标到下一个单词开头. e 移动光标到下一个单词结尾 b 移动光标到上一个单词. 0 移动光标到本 ...

  6. mysql使用错误

    mysql运行报The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time z ...

  7. C语言--第四次作业--数组

    1.本章学习总结 1.1 思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 不知不觉都快学习C语言结束了,自从开始了数组的学习就感觉难度瞬间几何级上升鸭(让人头大,感觉到了各种绝望), ...

  8. Vue+localstrong登录注册,并保持登录状态

    在router.js中添加meta区分 比如登录注册页面,不需要登录即可进入,那么我们把meta中的isLogin标志设置为false { //登录 path: '/login', component ...

  9. pycharm的list中clear的应用

    #清空的意思 li = [11,22,33,44] li.clear() print(li) #输出[],就是把列表清空

  10. 解决Spring boot中读取属性配置文件出现中文乱码的问题

    问题描述: 在配置文件application.properties中写了 server.port=8081 server.servlet.context-path=/boy name=张三 age=2 ...