Is it possible to minimize data type(int) size ?

Hello,

I am developing an application using java-networks programming. where i need to build packets of length of 64bits. the packet contains various fileds with various lengths. one field is named as sequence number, its length is 24bits . if i declared it as a int, it occupies 32bits, but i need only 24bits. How can i convert 32bits into 24 bits. is there any solutions for this ?.

sincerely

rama

[432 byte] By [mrcreddy] at [2007-9-19]
# 1
You appear to want to implement some custom protocol. You will have to take care of issues like byte order (endianness) etc. You can have your value in an int and ouput only its least significant 3 bytes.
BIJ at 2007-7-5 > top of java,Archived Forums,Java Programming [Archive]...
# 2

If you need this level of control over the layout of your packet, the packet must be defined as an array of bytes. When you want to put a 24-bit number in the packet, you split the number us list this:

int num42=0x00123456;

byte[] packet=new byte[1024];

byte[n+4]=(byte)((num24>>0)&0xff);

byte[n+5]=(byte)((num24>>8)&0xff);

byte[n+6]=(byte)((num24>>16)&0xff);

noe the packet will look lik: 0,0,0,0,0x56,0x34,0x12,0,0,0,0,0.....

Hope this helps.

armalcolm at 2007-7-5 > top of java,Archived Forums,Java Programming [Archive]...