devxlogo

Send an Integer through the Socket From a Java application to a C Application

Send an Integer through the Socket From a Java application to a C Application

When you want to send an integer through the socket from a Java application to a C application, or your Java application needs to save some integer in the file so the C application will read it, you need to convert one integer (int type ) to 4 bytes ( array of 4 bytes ). Here is a class that does the job:

 /** Performs conversions between integer ( long ) and 4 bytes and viceversa. */public class IntByteConverter{	private int intValue;	private byte[] byteArray;	public final static int mask = 256;/** IntByteConverter constructor . */public IntByteConverter() {	super();	byteArray = new byte[4];}/** IntByteConverter constructor. */public IntByteConverter(int theIntValue) {	super();	intValue = theIntValue;	byteArray = new byte[4];}/** Converts 4 bytes array to the int. */public void bytesToInt() {	int temp;	int  pMask = 1;	intValue = 0;	for( int i = 0; i 

size=3>

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist