`
crazier9527
  • 浏览: 1004896 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java 与 C 底层数据类型转换

    博客分类:
  • Java
阅读更多

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

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

相关推荐

    数据类型的转换数据类型的转换

    以下是对数据类型转换的详细阐述: 1. 基本数据类型:在大多数编程语言中,数据类型主要分为整型(如int)、浮点型(如float)、字符型(如char)、布尔型(如bool)和字符串型(如string)。这些类型各有其存储和...

    java 与 flex 数据转换

    以下是一些主要的数据类型转换规则: 1. **Null**: - 在 ActionScript 和 Java 中,`null` 类型是相同的,无需转换。 2. **Number**: - ActionScript 的 `Number` 类型可以映射到 Java 的 `java.lang.Number` ...

    Java Web应用底层数据传递模式的分析与研究.pdf

    总的来说,Java Web应用的底层数据传递是一个多层面、多步骤的过程,涉及到HTTP协议、Servlet、JSP、数据类型转换以及各种框架的使用。开发者需要深入了解这些技术,才能构建出高效、可维护的Web应用。通过对这一...

    Java与C语言混合开发技术

    - 在数据类型转换上,Java 与 C 的差异需要通过 JNI 提供的转换函数进行适配,如 Java 字符串到 C 字符数组的转换,以及 C 整型到 Java 整型的转换。 2. 异常处理: - Java 使用 try-catch 语句处理异常,而 C ...

    Linux下Java与C的数据交换

    - **类型映射**:Java和C的数据类型在映射时需要特别注意。例如: - Java的`boolean`类型映射到C的`jboolean`类型。 - Java的`byte`类型映射到C的`jbyte`类型。 - Java的`char`类型映射到C的`jchar`类型。 - ...

    java转换C生产dat文件

    在跨语言编程环境中,例如Java程序需要处理由C语言生成的数据文件(dat文件),常常会遇到数据类型不一致导致的数据解析问题,尤其是当涉及到二进制数据时。这是因为Java和C语言对于基本数据类型的处理方式存在差异...

    进制转换C源码

    本文将基于标题“进制转换C源码”和描述,深入探讨C语言实现不同进制转换的原理及代码实现,同时会涉及与Java语言进制转换差异的讨论。 首先,进制转换是计算机科学中的基本概念,主要包括二进制、八进制、十进制和...

    Jni 项目 包含各种数据类型转换

    `c/c++`则负责实现这些方法的具体逻辑,可能涉及更底层的数据处理或性能敏感的操作。 在项目中,`C++头文件在JAVA项目目录里.txt`可能包含有关如何在Java项目结构中组织C++头文件的信息。`JAVA idea项目`和`C++ vs...

    java的底层开发工具

    3. 在Java和C/C++之间传递数据,JNative提供了自动的类型转换。 三、示例 一个简单的例子是,创建一个Java类,声明一个native方法,然后使用JNative在C++中实现这个方法,例如计算两个整数的和: Java代码: ```...

    C转java工具

    C转Java工具通过解析C语言源代码,将其转换为Java语言的等价表达,从而实现了两者之间的桥梁。 该工具的基本工作原理是:首先,它会读取C源代码文件,然后对C语言的语法结构进行分析,包括变量声明、函数定义、循环...

    使用java和C语言进行通信 并且使用结构体传输的代码实例 包含三个示例

    在Java和C之间传递结构体时,通常需要进行数据类型转换。比如,Java的`long`对应C的`int64_t`,Java的`boolean`对应C的`int`等。因此,需要在两端都进行相应的类型匹配和转换。 7. **错误处理和内存管理** 由于...

    Android Java代码与JNI交互 引用类型转换(五)

    这通常涉及到数据类型的转换,因为Java和C/C++的数据类型并不完全相同。 在Java中,引用类型主要包括对象引用(Object)、类引用(Class)、数组引用(Array)等。当我们需要在JNI中处理这些引用时,需要进行适当的...

    J2EE(错误之数据类型转换异常)

    在J2EE编程中,我们经常会遇到数据类型转换异常,这通常是由于不正确的类型强制转换导致的。在给定的“名片管理系统”示例中,问题出在`setAttribute`和`getAttribute`方法的使用上。这两个方法是Java Servlet API的...

    Java与C语言混合编程方法探讨.pdf

    引入生成的头文件,并在C语言代码中实现对应的本地方法,利用JNIEnv指针和相应的类型定义,如`jobject`,来处理Java对象和数据。 5. 编译和运行: 编译C语言源代码,生成动态链接库文件,然后在Java程序中加载并...

    使用java与C写的jni小例子

    整个过程中,Java和C/C++之间的通信依赖于JNI框架,它处理了底层数据类型的转换、异常处理和内存管理等细节。这个小例子展示了如何利用JNI将Java的跨平台性和C/C++的高效性相结合,实现特定的程序需求。

    CPlus_to_Java_Converter;C++转java工具

    1. **数据类型转换**:C++中的指针在Java中通常对应于引用。转换器需要将C++的指针操作转换为Java的引用操作,同时处理内存分配和释放的差异,因为Java的垃圾回收机制会自动管理内存。 2. **面向对象的差异**:C++...

    java与c交互传递字符串和整型

    在Java与C/C++的交互中,字符串和整型是最基本的数据类型之一。下面我们将详细讨论如何在Java和C之间传递这两种类型。 1. **字符串传递**:在Java中,字符串是对象,而在C中,字符串通常表示为字符数组。在JNI中,...

    netty 在java中的字节码转换

    netty通信时经常和底层数据交互,C语言和java的数据类型和范围不同,通信时需要转化或兼容,附件为字节码、进制常用的转换类。

    AndroidJNI 学习 java与c传数据

    数据传输通常涉及类型转换,因为Java和C/C++的数据类型并不完全相同。 例如,假设我们有一个Java方法需要传递一个整型数组到C++,并在那里进行处理: ```java public class JNITest { public native void ...

Global site tag (gtag.js) - Google Analytics