Java 与 C 底层数据类型转换

Java代码   收藏代码
  1. import java.io.DataInputStream;  
  2. import java.io.DataOutputStream;  
  3. import java.io.IOException;  
  4.   
  5. /** 
  6.  * 仅仅适用于 Java 与 C++ 通讯中,网络流解析与生成使用 
  7.  * 
  8.  * 高低位互换(Big-Endian 大头在前 & Little-Endian 小头在前)。 
  9.  * 举例而言,有一个4字节的数据0x01020304,要存储在内存中或文件中编号0˜3字节的位置,两种字节序的排列方式分别如下: 
  10.  * <pre> 
  11.  * Big Endian 
  12.  *   
  13.  * 低地址                           高地址 
  14.  * ----------------------------------------------------&gt; 
  15.  * 地址编号 
  16.  * |     0      |      1     |     2       |      3    | 
  17.  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  18.  * |     01     |      02    |     03      |     04    | 
  19.  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  20.  *  
  21.  * Little Endian 
  22.  *  
  23.  * 低地址                           高地址 
  24.  * ----------------------------------------------------&gt; 
  25.  * 地址编号 
  26.  * |     0      |      1     |     2       |      3    | 
  27.  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  28.  * |     04     |      03    |     02      |     01    | 
  29.  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 
  30.  * </pre> 
  31.  * Java则统一使用big模式 
  32.  * c中的unsigned short  对应着java中的char两个字节,无符号 
  33.  * c的无符号int,short,byte字节数组,相应转换成java的long,char,short 
  34.  * 
  35.  * @author Snowolf 
  36.  * @version 1.0 
  37.  * @since 1.0 
  38.  */  
  39. public abstract class CIOUtil {  
  40.     public static final String CHARSET = "UTF-8";  
  41.   
  42.     /** 
  43.      * 从输入流中读布尔  
  44.      *  
  45.      * @param is 
  46.      * @return 
  47.      * @throws IOException 
  48.      */  
  49.     public static boolean readBoolean(DataInputStream is) throws IOException {  
  50.         return is.readBoolean();  
  51.     }  
  52.   
  53.     /** 
  54.      * 从流中读定长度字节数组 
  55.      *  
  56.      * @param is 
  57.      * @param s 
  58.      * @return 
  59.      * @throws IOException 
  60.      */  
  61.     public static byte[] readBytes(DataInputStream is, int i)  
  62.             throws IOException {  
  63.         byte[] data = new byte[i];  
  64.         is.readFully(data);  
  65.   
  66.         return data;  
  67.     }  
  68.   
  69.     /** 
  70.      * 从输入流中读字符  
  71.      *  
  72.      * @param is 
  73.      * @return 
  74.      * @throws IOException 
  75.      */  
  76.     public static char readChar(DataInputStream is) throws IOException {  
  77.         return (char) readShort(is);  
  78.     }  
  79.   
  80.     /** 
  81.      * 从输入流中读双精度  
  82.      *  
  83.      * @param is 
  84.      * @return 
  85.      * @throws IOException 
  86.      */  
  87.     public static double readDouble(DataInputStream is) throws IOException {  
  88.         return Double.longBitsToDouble(readLong(is));  
  89.     }  
  90.   
  91.     /** 
  92.      * 从输入流中读单精度 
  93.      *  
  94.      * @param is 
  95.      * @return 
  96.      * @throws IOException 
  97.      */  
  98.     public static float readFloat(DataInputStream is) throws IOException {  
  99.         return Float.intBitsToFloat(readInt(is));  
  100.     }  
  101.   
  102.     /** 
  103.      * 从流中读整型 
  104.      *  
  105.      * @param is 
  106.      * @return 
  107.      * @throws IOException 
  108.      */  
  109.     public static int readInt(DataInputStream is) throws IOException {  
  110.         return Integer.reverseBytes(is.readInt());  
  111.     }  
  112.   
  113.     /** 
  114.      * 从流中读长整型 
  115.      *  
  116.      * @param is 
  117.      * @return 
  118.      * @throws IOException 
  119.      */  
  120.     public static long readLong(DataInputStream is) throws IOException {  
  121.         return Long.reverseBytes(is.readLong());  
  122.     }  
  123.   
  124.     /** 
  125.      * 从流中读短整型 
  126.      *  
  127.      * @param is 
  128.      * @return 
  129.      * @throws IOException 
  130.      */  
  131.     public static short readShort(DataInputStream is) throws IOException {  
  132.         return Short.reverseBytes(is.readShort());  
  133.     }  
  134.   
  135.     /** 
  136.      * 从输入流中读字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串 
  137.      *  
  138.      * @param is 
  139.      * @return 
  140.      * @throws IOException 
  141.      */  
  142.     public static String readUTF(DataInputStream is) throws IOException {  
  143.         short s = readShort(is);  
  144.         byte[] str = new byte[s];  
  145.   
  146.         is.readFully(str);  
  147.   
  148.         return new String(str, CHARSET);  
  149.     }  
  150.   
  151.     /** 
  152.      * 向输出流中写布尔 
  153.      *  
  154.      * @param os 
  155.      * @param b 
  156.      * @throws IOException 
  157.      */  
  158.     public static void writeBoolean(DataOutputStream os, boolean b)  
  159.             throws IOException {  
  160.         os.writeBoolean(b);  
  161.     }  
  162.   
  163.     /** 
  164.      * 向输出流中写字节数组 
  165.      *  
  166.      * @param os 
  167.      * @param data 
  168.      * @throws IOException 
  169.      */  
  170.     public static void writeBytes(DataOutputStream os, byte[] data)  
  171.             throws IOException {  
  172.         os.write(data);  
  173.     }  
  174.   
  175.     /** 
  176.      * 向输出流中写字符 
  177.      *  
  178.      * @param os 
  179.      * @param b 
  180.      * @throws IOException 
  181.      */  
  182.     public static void writeChar(DataOutputStream os, char b)  
  183.             throws IOException {  
  184.         writeShort(os, (short) b);  
  185.     }  
  186.   
  187.     /** 
  188.      * 向输出流中写双精度 
  189.      *  
  190.      * @param os 
  191.      * @param d 
  192.      * @throws IOException 
  193.      */  
  194.     public static void writeDouble(DataOutputStream os, double d)  
  195.             throws IOException {  
  196.         writeLong(os, Double.doubleToLongBits(d));  
  197.     }  
  198.   
  199.     /** 
  200.      * 向输出流中写单精度 
  201.      *  
  202.      * @param os 
  203.      * @param f 
  204.      * @throws IOException 
  205.      */  
  206.     public static void writeFloat(DataOutputStream os, float f)  
  207.             throws IOException {  
  208.         writeInt(os, Float.floatToIntBits(f));  
  209.     }  
  210.   
  211.     /** 
  212.      * 向输出流中写整型 
  213.      *  
  214.      * @param os 
  215.      * @param i 
  216.      * @throws IOException 
  217.      */  
  218.     public static void writeInt(DataOutputStream os, int i) throws IOException {  
  219.         os.writeInt(Integer.reverseBytes(i));  
  220.     }  
  221.   
  222.     /** 
  223.      * 向输出流中写长整型 
  224.      *  
  225.      * @param os 
  226.      * @param l 
  227.      * @throws IOException 
  228.      */  
  229.     public static void writeLong(DataOutputStream os, long l)  
  230.             throws IOException {  
  231.         os.writeLong(Long.reverseBytes(l));  
  232.     }  
  233.   
  234.     /** 
  235.      * 向输出流中写短整型 
  236.      *  
  237.      * @param os 
  238.      * @param s 
  239.      * @throws IOException 
  240.      */  
  241.     public static void writeShort(DataOutputStream os, short s)  
  242.             throws IOException {  
  243.         os.writeShort(Short.reverseBytes(s));  
  244.     }  
  245.   
  246.     /** 
  247.      * 向输出流中写字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串 
  248.      *  
  249.      * @param os 
  250.      * @param str 
  251.      * @throws IOException 
  252.      */  
  253.     public static void writeUTF(DataOutputStream os, String str)  
  254.             throws IOException {  
  255.         byte[] data = str.getBytes(CHARSET);  
  256.         writeShort(os, (short) data.length);  
  257.         os.write(data);  
  258.     }  
  259.   
  260. }  

再写个测试类 
Java代码   收藏代码
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import org.junit.Test;  
  8. import static org.junit.Assert.*;  
  9.   
  10. /** 
  11.  *  
  12.  * @author Snowolf 
  13.  * @version 1.0 
  14.  * @since 1.0 
  15.  */  
  16. public class CIOUtilTest {  
  17.     /** 
  18.      * 测试布尔值 
  19.      *  
  20.      * @throws IOException 
  21.      */  
  22.     @Test  
  23.     public final void testBoolean() throws IOException {  
  24.         boolean input = true;  
  25.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  26.         DataOutputStream os = new DataOutputStream(baos);  
  27.   
  28.         CIOUtil.writeBoolean(os, input);  
  29.   
  30.         byte[] b = baos.toByteArray();  
  31.         baos.flush();  
  32.         baos.close();  
  33.   
  34.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  35.         DataInputStream is = new DataInputStream(bais);  
  36.   
  37.         boolean output = CIOUtil.readBoolean(is);  
  38.   
  39.         bais.close();  
  40.   
  41.         assertEquals(input, output);  
  42.     }  
  43.   
  44.     /** 
  45.      * 测试字节数组 
  46.      *  
  47.      * @throws IOException 
  48.      */  
  49.     @Test  
  50.     public final void testBytes() throws IOException {  
  51.         byte[] input = "中文".getBytes("UTF-8");  
  52.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  53.         DataOutputStream os = new DataOutputStream(baos);  
  54.   
  55.         CIOUtil.writeBytes(os, input);  
  56.   
  57.         byte[] b = baos.toByteArray();  
  58.         baos.flush();  
  59.         baos.close();  
  60.   
  61.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  62.         DataInputStream is = new DataInputStream(bais);  
  63.   
  64.         byte[] output = CIOUtil.readBytes(is, 6);  
  65.   
  66.         bais.close();  
  67.   
  68.         assertArrayEquals(input, output);  
  69.     }  
  70.   
  71.     /** 
  72.      * 测试字符 
  73.      *  
  74.      * @throws IOException 
  75.      */  
  76.     @Test  
  77.     public final void testChar() throws IOException {  
  78.         char input = '中';  
  79.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  80.         DataOutputStream os = new DataOutputStream(baos);  
  81.   
  82.         CIOUtil.writeChar(os, input);  
  83.   
  84.         byte[] b = baos.toByteArray();  
  85.         baos.flush();  
  86.         baos.close();  
  87.   
  88.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  89.         DataInputStream is = new DataInputStream(bais);  
  90.   
  91.         char output = CIOUtil.readChar(is);  
  92.   
  93.         bais.close();  
  94.   
  95.         assertEquals(input, output);  
  96.     }  
  97.   
  98.     /** 
  99.      * 测试双精度 
  100.      *  
  101.      * @throws IOException 
  102.      */  
  103.     @Test  
  104.     public final void testDouble() throws IOException {  
  105.         double input = 1.23456789d;  
  106.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  107.         DataOutputStream os = new DataOutputStream(baos);  
  108.   
  109.         CIOUtil.writeDouble(os, input);  
  110.   
  111.         byte[] b = baos.toByteArray();  
  112.         baos.flush();  
  113.         baos.close();  
  114.   
  115.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  116.         DataInputStream is = new DataInputStream(bais);  
  117.   
  118.         double output = CIOUtil.readDouble(is);  
  119.   
  120.         bais.close();  
  121.   
  122.         assertEquals(input, output, 9);  
  123.     }  
  124.   
  125.     /** 
  126.      * 测试单精度 
  127.      *  
  128.      * @throws IOException 
  129.      */  
  130.     @Test  
  131.     public final void testFloat() throws IOException {  
  132.         float input = 1.23456789f;  
  133.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  134.         DataOutputStream os = new DataOutputStream(baos);  
  135.   
  136.         CIOUtil.writeFloat(os, input);  
  137.   
  138.         byte[] b = baos.toByteArray();  
  139.         baos.flush();  
  140.         baos.close();  
  141.   
  142.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  143.         DataInputStream is = new DataInputStream(bais);  
  144.   
  145.         float output = CIOUtil.readFloat(is);  
  146.   
  147.         bais.close();  
  148.   
  149.         assertEquals(input, output, 9);  
  150.     }  
  151.   
  152.     /** 
  153.      * 测试整型 
  154.      *  
  155.      * @throws IOException 
  156.      */  
  157.     @Test  
  158.     public final void testInt() throws IOException {  
  159.         int input = 1;  
  160.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  161.         DataOutputStream os = new DataOutputStream(baos);  
  162.   
  163.         CIOUtil.writeInt(os, input);  
  164.   
  165.         byte[] b = baos.toByteArray();  
  166.         baos.flush();  
  167.         baos.close();  
  168.   
  169.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  170.         DataInputStream is = new DataInputStream(bais);  
  171.   
  172.         int output = CIOUtil.readInt(is);  
  173.   
  174.         bais.close();  
  175.   
  176.         assertEquals(input, output);  
  177.     }  
  178.   
  179.     /** 
  180.      * 测试长整型 
  181.      *  
  182.      * @throws IOException 
  183.      */  
  184.     @Test  
  185.     public final void testLong() throws IOException {  
  186.         long input = 1l;  
  187.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  188.         DataOutputStream os = new DataOutputStream(baos);  
  189.   
  190.         CIOUtil.writeLong(os, input);  
  191.   
  192.         byte[] b = baos.toByteArray();  
  193.         baos.flush();  
  194.         baos.close();  
  195.   
  196.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  197.         DataInputStream is = new DataInputStream(bais);  
  198.   
  199.         long output = CIOUtil.readLong(is);  
  200.   
  201.         bais.close();  
  202.   
  203.         assertEquals(input, output);  
  204.     }  
  205.   
  206.     /** 
  207.      * 测试短整型 
  208.      *  
  209.      * @throws IOException 
  210.      */  
  211.     @Test  
  212.     public final void testShort() throws IOException {  
  213.         short input = 1;  
  214.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  215.         DataOutputStream os = new DataOutputStream(baos);  
  216.   
  217.         CIOUtil.writeShort(os, input);  
  218.   
  219.         byte[] b = baos.toByteArray();  
  220.         baos.flush();  
  221.         baos.close();  
  222.   
  223.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  224.         DataInputStream is = new DataInputStream(bais);  
  225.   
  226.         short output = CIOUtil.readShort(is);  
  227.   
  228.         bais.close();  
  229.   
  230.         assertEquals(input, output);  
  231.     }  
  232.   
  233.     /** 
  234.      * 测试UTF-8字符串 
  235.      *  
  236.      * @throws IOException 
  237.      */  
  238.     @Test  
  239.     public final void testUTF() throws IOException {  
  240.         String input = "中文支持";  
  241.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  242.         DataOutputStream os = new DataOutputStream(baos);  
  243.   
  244.         CIOUtil.writeUTF(os, input);  
  245.   
  246.         byte[] b = baos.toByteArray();  
  247.         baos.flush();  
  248.         baos.close();  
  249.   
  250.         ByteArrayInputStream bais = new ByteArrayInputStream(b);  
  251.         DataInputStream is = new DataInputStream(bais);  
  252.   
  253.         String output = CIOUtil.readUTF(is);  
  254.   
  255.         bais.close();  
  256.   
  257.         assertEquals(input, output);  
  258.     }  
  259.   
  260. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值