前段时间一直忙着做J2EE服务器与C++客户端的项目。终于,项目告一段落,有一些收获 在这里与大家分享。

Java代码 

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* 仅仅适用于 Java 与 C++ 通讯中,网络流解析与生成使用
*
* 高低位互换(Big-Endian 大头在前 & Little-Endian 小头在前)。
* 举例而言,有一个4字节的数据0x01020304,要存储在内存中或文件中编号 0˜3字节的位置,两种字节序的排列方式分别如下:
*
* Big Endian
*
* 低地址              高地址
* ---------------------------------------------------->
* 地址编号
* |   0   |   1   |   2    |   3  |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |   01   |   02  |   03   |   04  |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Little Endian
*
* 低地址              高地址
* ---------------------------------------------------->
* 地址编号
* |   0   |   1   |   2    |   3  |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |   04   |   03  |   02   |   01  |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Java则统一使用big模式
* c中的unsigned short 对应着java中的char两个字节,无符号
* c的无符号int,short,byte字节数组,相应转换成java的long,char,short
*
* @author Snowolf
* @version 1.0
* @since 1.0
*/
public abstract class CIOUtil {
public static final String CHARSET = "UTF-8";
/**
* 从输入流中读布尔 
*
* @param is
* @return
* @throws IOException
*/
public static boolean readBoolean(DataInputStream is) throws IOException  {
return is.readBoolean();
}
/**
* 从流中读定长度字节数组
*
* @param is
* @param s
* @return
* @throws IOException
*/
public static byte[] readBytes(DataInputStream is, int i)
throws IOException {
byte[] data = new byte[i];
is.readFully(data);
return data;
}
/**
* 从输入流中读字符 
*
* @param is
* @return
* @throws IOException
*/
public static char readChar(DataInputStream is) throws IOException {
return (char) readShort(is);
}
/**
* 从输入流中读双精度 
*
* @param is
* @return
* @throws IOException
*/
public static double readDouble(DataInputStream is) throws IOException  {
return Double.longBitsToDouble(readLong(is));
}
/**
* 从输入流中读单精度
*
* @param is
* @return
* @throws IOException
*/
public static float readFloat(DataInputStream is) throws IOException  {
return Float.intBitsToFloat(readInt(is));
}
/**
* 从流中读整型
*
* @param is
* @return
* @throws IOException
*/
public static int readInt(DataInputStream is) throws IOException {
return Integer.reverseBytes(is.readInt());
}
/**
* 从流中读长整型
*
* @param is
* @return
* @throws IOException
*/
public static long readLong(DataInputStream is) throws IOException {
return Long.reverseBytes(is.readLong());
}
/**
* 从流中读短整型
*
* @param is
* @return
* @throws IOException
*/
public static short readShort(DataInputStream is) throws IOException  {
return Short.reverseBytes(is.readShort());
}
/**
* 从输入流中读字符串 字符串 结构为一个指定字符串字节长度的短整型+实际字符 串
*
* @param is
* @return
* @throws IOException
*/
public static String readUTF(DataInputStream is) throws IOException  {
short s = readShort(is);
byte[] str = new byte[s];
is.readFully(str);
return new String(str, CHARSET);
}
/**
* 向输出流中写布尔
*
* @param os
* @param b
* @throws IOException
*/
public static void writeBoolean(DataOutputStream os, boolean b)
throws IOException {
os.writeBoolean(b);
}
/**
* 向输出流中写字节数组
*
* @param os
* @param data
* @throws IOException
*/
public static void writeBytes(DataOutputStream os, byte[] data)
throws IOException {
os.write(data);
}
/**
* 向输出流中写字符
*
* @param os
* @param b
* @throws IOException
*/
public static void writeChar(DataOutputStream os, char b)
throws IOException {
writeShort(os, (short) b);
}
/**
* 向输出流中写双精度
*
* @param os
* @param d
* @throws IOException
*/
public static void writeDouble(DataOutputStream os, double d)
throws IOException {
writeLong(os, Double.doubleToLongBits(d));
}
/**
* 向输出流中写单精度
*
* @param os
* @param f
* @throws IOException
*/
public static void writeFloat(DataOutputStream os, float f)
throws IOException {
writeInt(os, Float.floatToIntBits(f));
}
/**
* 向输出流中写整型
*
* @param os
* @param i
* @throws IOException
*/
public static void writeInt(DataOutputStream os, int i) throws  IOException {
os.writeInt(Integer.reverseBytes(i));
}
/**
* 向输出流中写长整型
*
* @param os
* @param l
* @throws IOException
*/
public static void writeLong(DataOutputStream os, long l)
throws IOException {
os.writeLong(Long.reverseBytes(l));
}
/**
* 向输出流中写短整型
*
* @param os
* @param s
* @throws IOException
*/
public static void writeShort(DataOutputStream os, short s)
throws IOException {
os.writeShort(Short.reverseBytes(s));
}
/**
* 向输出流中写字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际 字符串
*
* @param os
* @param str
* @throws IOException
*/
public static void writeUTF(DataOutputStream os, String str)
throws IOException {
byte[] data = str.getBytes(CHARSET);
writeShort(os, (short) data.length);
os.write(data);
}
}

再写个测试类

Java代码

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Snowolf
* @version 1.0
* @since 1.0
*/
public class CIOUtilTest {
/**
* 测试布尔值
*
* @throws IOException
*/
@Test
public final void testBoolean() throws IOException {
boolean input = true;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeBoolean(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
boolean output = CIOUtil.readBoolean(is);
bais.close();
assertEquals(input, output);
}
/**
* 测试字节数组
*
* @throws IOException
*/
@Test
public final void testBytes() throws IOException {
byte[] input = "中文".getBytes("UTF-8");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeBytes(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
byte[] output = CIOUtil.readBytes(is, 6);
bais.close();
assertArrayEquals(input, output);
}
/**
* 测试字符
*
* @throws IOException
*/
@Test
public final void testChar() throws IOException {
char input = '中';
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeChar(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
char output = CIOUtil.readChar(is);
bais.close();
assertEquals(input, output);
}
/**
* 测试双精度
*
* @throws IOException
*/
@Test
public final void testDouble() throws IOException {
double input = 1.23456789d;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeDouble(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
double output = CIOUtil.readDouble(is);
bais.close();
assertEquals(input, output, 9);
}
/**
* 测试单精度
*
* @throws IOException
*/
@Test
public final void testFloat() throws IOException {
float input = 1.23456789f;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeFloat(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
float output = CIOUtil.readFloat(is);
bais.close();
assertEquals(input, output, 9);
}
/**
* 测试整型
*
* @throws IOException
*/
@Test
public final void testInt() throws IOException {
int input = 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeInt(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
int output = CIOUtil.readInt(is);
bais.close();
assertEquals(input, output);
}
/**
* 测试长整型
*
* @throws IOException
*/
@Test
public final void testLong() throws IOException {
long input = 1l;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeLong(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
long output = CIOUtil.readLong(is);
bais.close();
assertEquals(input, output);
}
/**
* 测试短整型
*
* @throws IOException
*/
@Test
public final void testShort() throws IOException {
short input = 1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeShort(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
short output = CIOUtil.readShort(is);
bais.close();
assertEquals(input, output);
}
/**
* 测试UTF-8字符串
*
* @throws IOException
*/
@Test
public final void testUTF() throws IOException {
String input = "中文支持";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
CIOUtil.writeUTF(os, input);
byte[] b = baos.toByteArray();
baos.flush();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream is = new DataInputStream(bais);
String output = CIOUtil.readUTF(is);
bais.close();
assertEquals(input, output);
}
}

Java与C底层数据类型转换相关推荐

  1. Java String常用的数据类型转换

    工作写代码经常遇到string的数据类型转换,每次都去搜索如何转换很不方便.写篇博客做个总结,以后看这篇就好了. 1.string-bigDecimal 2.string-date @Testpubl ...

  2. JAVA基本数据类型、数据类型转换

    JAVA中数据类型 一.基本数据类型 数值型 整型 byte 字节型 占一个字节 用8位存储数据 范围:-128~127 默认值0 short 短整型 占2个字节 2^15 范围:-32768~327 ...

  3. Java(2)数据类型转换、变量和常量

    数据类型 1.整型 int(4个字节), 短整型short(2个字节),长整型long(8个字节),字节型byte(1个字节). 2.字符型char(2个字节) 3.浮点型单精度float(4个字节) ...

  4. java tryparse用法_数据类型转换的三种方式 Convert,parse和TryParse的解析

    以Int类型为例,具体说明Convert.ToInt32(object value),int.Parse(object value)和int.TryParse(string s,out int res ...

  5. java 类型转换方法_java数据类型转换的常见方法

    public class Testfun { public static void main(String[] args) { // (一)跨Number父类的类型转换 // 1.str转int =& ...

  6. JNI - JAVA 数据类型转换

    基本数据类型转换 在 Java 中传递的参数类型是 int,而在 JNI 中就成了 jint,这就涉及到 Java 到 JNI 的数据类型转换. Java 类型 Native 类型 字节长度 bool ...

  7. java 数据类型转换的一场_Java基础 — 四类八种基本数据类型

    整型:整数类型int 一般的数据. long 极大的数据. short 用于特定的场合,比如底层的文件处理或者需要控制占用存储单元空间量的大数组. byte 用于特定的场合,比如底层的文件处理或者需要 ...

  8. java中数据类型转换、ASCII编码

    数据类型转换: JAVA语言中要求参与计算的数据类型要保持一致,如果不一致则会发生数据类型转换,数据类型转换可分为:自动类型转换(隐式转换)和强制类型转换(显式转换). 自动类型转换:代码不需要特殊处 ...

  9. JAVA中两个char类型相加_5.16--java数据类型转换及杂记

    我们先来回顾一下前面学的java中的数据类型: java中数据类型分为基本数据类型和引用型数据类型 8种基本数据类型(值类型) 整型:byte(-128~127).short.int.long lon ...

最新文章

  1. 图灵访谈系列之九:CNode社区谈Node.js技术及生态
  2. mysql mysqldump只导出表结构或只导出数据的实现方法
  3. js的时间函数实现一个电子表
  4. 爱酷pro充电测试软件,iQOO 5 Pro续航、充电测试简报
  5. 【poj1742】 Coins
  6. springboot整合视图层之freemarker
  7. Web开发中实用小工具
  8. C++回声服务器_6-多进程pipe版本服务器
  9. caffe-SSD源码解析——生成数据列表及数据集
  10. linux定时备份Mysql
  11. Java 设计模式 之 观察者模式(Observer)
  12. QTP10.0安装所遇问题-脚本调试器问题
  13. 零基础搭建双端影视盒子——二、搭建影视管理后台
  14. Python爬取豆瓣网图书评论
  15. pdf文件过大如何缩小但保证清晰度
  16. Requirement already satisfied
  17. 信息安全知识竞赛试题
  18. 在linux终端下使用scp与远程windows传输文件
  19. 2020京东校园招聘笔试编码题小分享--大小写切换
  20. 【Ray Trace from Groud Up】光线追踪代码实现解析

热门文章

  1. LiveVideoStack线上分享第四季(三):在线教育的音视频架构设计及弱网对抗技术...
  2. RTMP之后,SRT与QUIC
  3. 手淘H265编解码算法与工程优化
  4. FFmpeg 硬件加速方案概览 (上)
  5. 2017-2018:WebRTC标准演进与发展瓶颈
  6. FFmpeg过滤器框架分析
  7. Ubuntu增加一个用户并给普通用户赋予root权限的方法
  8. 确保对象的唯一性——单例模式
  9. 【人工智能导论】遗传算法求解TSP问题(含源码github)
  10. CentOS+tomcat jsp笔记