I am sending integer from c to java .
In C , integer is 24565. I converted it to 4 bytes. Its byte conversion is
First Byte: -11
Second Byte: 95
Third Byte: 0
Fourth Byte: 0.
My C code is
char content_size_ch ars[4];
int content_size=24 565;
// Convert output length to bytes
content_size_ch ars[0] = content_size & 0xFF;
content_size_ch ars[1] = (content_size & 0xFF00) >> 8;
content_size_ch ars[2] = (content_size & 0xFF0000) >> 16;
content_size_ch ars[3] = (content_size & 0xFF000000) >> 24;
// write byte array to stderr stream
fprintf(stderr, "%d",content_si ze_chars[0] );
fprintf(stderr, "%d",content_si ze_chars[1] );
fprintf(stderr, "%d",content_si ze_chars[2] );
fprintf(stderr, "%d",content_si ze_chars[3] );
fflush(stderr);
In java, bytes read using datainputstream are
byte []lengthBytes=new byte[4];
lengthBytes[0]=dataInStream.r eadByte();
lengthBytes[1]=dataInStream.r eadByte();
lengthBytes[2]=dataInStream.r eadByte();
lengthBytes[3]=dataInStream.r eadByte();
Output is :
First byte: -
Second Byte: 1
Third Byte: 1
Fourth Byte: 9
How to send integer from C to java ?
Thanks
In C , integer is 24565. I converted it to 4 bytes. Its byte conversion is
First Byte: -11
Second Byte: 95
Third Byte: 0
Fourth Byte: 0.
My C code is
char content_size_ch ars[4];
int content_size=24 565;
// Convert output length to bytes
content_size_ch ars[0] = content_size & 0xFF;
content_size_ch ars[1] = (content_size & 0xFF00) >> 8;
content_size_ch ars[2] = (content_size & 0xFF0000) >> 16;
content_size_ch ars[3] = (content_size & 0xFF000000) >> 24;
// write byte array to stderr stream
fprintf(stderr, "%d",content_si ze_chars[0] );
fprintf(stderr, "%d",content_si ze_chars[1] );
fprintf(stderr, "%d",content_si ze_chars[2] );
fprintf(stderr, "%d",content_si ze_chars[3] );
fflush(stderr);
In java, bytes read using datainputstream are
byte []lengthBytes=new byte[4];
lengthBytes[0]=dataInStream.r eadByte();
lengthBytes[1]=dataInStream.r eadByte();
lengthBytes[2]=dataInStream.r eadByte();
lengthBytes[3]=dataInStream.r eadByte();
Output is :
First byte: -
Second Byte: 1
Third Byte: 1
Fourth Byte: 9
How to send integer from C to java ?
Thanks
Comment