把字符串和整数放到二进制流
- private void valuecpy(byte[] b,int start, int end,String strMsg){
- byte[] cs = strMsg.getBytes();
- for(byte c : cs){
- b[start] = c;
- start++;
- if(start>=end)
- break;
- }
- }
- private void valuecpy(byte[] b,int start, int end,int nValue){
- b[start] =(byte)(nValue&0x000000ff) ;
- b[start+1]=(byte)( (nValue&0x0000ff00) >> 8) ;
- b[start+2]=(byte)( (nValue&0x00ff0000) >> 16) ;
- b[start+3]=(byte)( (nValue&0xff000000) >> 24) ;
- }
从二进制流中取字符串和整数
- private int getIntValue(byte [] b,int s,int e)
- {
- int nR=0;
- if(s+3<e)
- {
- nR = b[s] & 0xff;
- nR = nR + ( b[s+1]<<8 & 0x0000ff00 );
- nR = nR + ( b[s+2]<<16 & 0x00ff0000 );
- nR = nR + ( b[s+3]<<24 & 0xff000000 );
- }
- return nR;
- }
- private String getStrValue(byte[] b,int s,int e)
- {
- byte[] tb=new byte[e-s+1];
- int i=0;
- for(byte ee:tb)
- {
- tb[i++]=0;
- }
- i=0;
- for(byte ee:b)
- {
- if(i>=s && i<e)
- {
- tb[i-s]= b[i];
- }
- else if(i>=e)
- {
- break;
- }
- i++;
- }
- String r=new String(tb);
- return r;
- }
其它可能要用到的函数
- private void ZeroMem()
- {
- int i=0;
- m_packet = new byte[1024];
- for(byte c:m_packet)
- {
- m_packet[i++]=0;
- }
- }
这样C语言中struct结构体变量可以对应到二进制流,而Java程序就可以从二进制流中取数据