Java与C底层数据类型转换 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-17
ayOutputStream 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);
}
} |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于Java与C底层数据类型转换 - 编程入门网的所有评论