http://www.cppblog.com/woaidongmao/archive/2009/08/04/92147.aspx
2010-04-20 17:36
531 查看
文章出处:
最近在学习一些SOCKET通讯协议设计的知识,涉及到了一些协议的设计,需要打包和解包,所以把一些比较基本的知识搜集了下来.
暂时把协议结构定义得比较简单,也就是 CMD(命令类型8位)+序号(整形16位)+接收人长度(整形16位)+消息内容(每个包只接收1024位消息内容,多余的需要分割);下面是需要用到的一些方法.
//整数到字节数组的转换
public byte[] intToByte(int intValue) {
byte[] result = new byte[4];
result[0] = (byte) ( (intValue & 0xFF000000) >> 24);
result[1] = (byte) ( (intValue & 0x00FF0000) >> 16);
result[2] = (byte) ( (intValue & 0x0000FF00) >> 8);
result[3] = (byte) ( (intValue & 0x000000FF));
return result;
}
//字节数组到整数的转换
public static int byteToInt(byte[] b) {
public static int byteToInt(byte[] byteVal) {
int result = 0;
for (int i = 0; i < byteVal.length; i++) {
int tmpVal = (byteVal[i] << (8 * (3 - i)));
switch (i) {
case 0:
tmpVal = tmpVal & 0xFF000000;
break;
case 1:
tmpVal = tmpVal & 0x00FF0000;
break;
case 2:
tmpVal = tmpVal & 0x0000FF00;
break;
case 3:
tmpVal = tmpVal & 0x000000FF;
break;
}
result = result | tmpVal;
}
return result;
}
//字符到字节转换
public static byte[] charToByte(char ch){
int temp=(int)ch;
byte[] b=new byte[2];
for (int i=b.length-1;i>-1;i--){
b = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}
//字节到字符转换
public static char byteToChar(byte[] b){
int s=0;
if(b[0]>0)
s+=b[0];
else
s+=256+b[0];
s*=256;
if(b[1]>0)
s+=b[1];
else
s+=256+b[1];
char ch=(char)s;
return ch;
}
//浮点到字节转换
public static byte[] doubleToByte(double d){
byte[] b=new byte[8];
long l=Double.doubleToLongBits(d);
for(int i=0;i<b.length;i++){
b=new Long(l).byteValue();
l=l>>8;
}
return b;
}
//字节到浮点转换
public static double byteToDouble(byte[] b){
long l;
l=b[0];
l&=0xff;
l|=((long)b[1]<<8);
l&=0xffff;
l|=((long)b[2]<<16);
l&=0xffffff;
l|=((long)b[3]<<24);
l&=0xffffffffl;
l|=((long)b[4]<<32);
l&=0xffffffffffl;
l|=((long)b[5]<<40);
l&=0xffffffffffffl;
l|=((long)b[6]<<48);
l&=0xffffffffffffffl;
l|=((long)b[7]<<56);
return Double.longBitsToDouble(l);
}
最近在学习一些SOCKET通讯协议设计的知识,涉及到了一些协议的设计,需要打包和解包,所以把一些比较基本的知识搜集了下来.
暂时把协议结构定义得比较简单,也就是 CMD(命令类型8位)+序号(整形16位)+接收人长度(整形16位)+消息内容(每个包只接收1024位消息内容,多余的需要分割);下面是需要用到的一些方法.
//整数到字节数组的转换
public byte[] intToByte(int intValue) {
byte[] result = new byte[4];
result[0] = (byte) ( (intValue & 0xFF000000) >> 24);
result[1] = (byte) ( (intValue & 0x00FF0000) >> 16);
result[2] = (byte) ( (intValue & 0x0000FF00) >> 8);
result[3] = (byte) ( (intValue & 0x000000FF));
return result;
}
//字节数组到整数的转换
public static int byteToInt(byte[] b) {
public static int byteToInt(byte[] byteVal) {
int result = 0;
for (int i = 0; i < byteVal.length; i++) {
int tmpVal = (byteVal[i] << (8 * (3 - i)));
switch (i) {
case 0:
tmpVal = tmpVal & 0xFF000000;
break;
case 1:
tmpVal = tmpVal & 0x00FF0000;
break;
case 2:
tmpVal = tmpVal & 0x0000FF00;
break;
case 3:
tmpVal = tmpVal & 0x000000FF;
break;
}
result = result | tmpVal;
}
return result;
}
//字符到字节转换
public static byte[] charToByte(char ch){
int temp=(int)ch;
byte[] b=new byte[2];
for (int i=b.length-1;i>-1;i--){
b = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}
//字节到字符转换
public static char byteToChar(byte[] b){
int s=0;
if(b[0]>0)
s+=b[0];
else
s+=256+b[0];
s*=256;
if(b[1]>0)
s+=b[1];
else
s+=256+b[1];
char ch=(char)s;
return ch;
}
//浮点到字节转换
public static byte[] doubleToByte(double d){
byte[] b=new byte[8];
long l=Double.doubleToLongBits(d);
for(int i=0;i<b.length;i++){
b=new Long(l).byteValue();
l=l>>8;
}
return b;
}
//字节到浮点转换
public static double byteToDouble(byte[] b){
long l;
l=b[0];
l&=0xff;
l|=((long)b[1]<<8);
l&=0xffff;
l|=((long)b[2]<<16);
l&=0xffffff;
l|=((long)b[3]<<24);
l&=0xffffffffl;
l|=((long)b[4]<<32);
l&=0xffffffffffl;
l|=((long)b[5]<<40);
l&=0xffffffffffffl;
l|=((long)b[6]<<48);
l&=0xffffffffffffffl;
l|=((long)b[7]<<56);
return Double.longBitsToDouble(l);
}
相关文章推荐
- Windows Mobile 发送短信的问题(转自http://www.cppblog.com/SpringSnow/archive/2009/06/08/76107.html)
- TThread 类 详细解析 --(原帖 华夏黑客同盟)http://www.cppblog.com/Khan/archive/2006/11/21/15503.html
- GDB 单步调试 http://www.cppblog.com/baby-fly/archive/2010/07/27/121395.html
- PostThreadMessage http://www.cppblog.com/sandy/archive/2005/12/31/2320.html
- LIB和DLL的区别、建立及使用http://www.cppblog.com/amazon/archive/2009/09/04/95318.html
- 转换汉字为unicode形式的字符串和转换unicode形式字符串转换成汉字__http://www.cppblog.com/biao/archive/2010/12/21/137087.html
- windows核心编程--内存结构 http://www.cppblog.com/mzty/archive/2006/09/20/12764.html
- 文件操作 C++ ;;http://www.cppblog.com/niewenlong/archive/2007/06/14/26302.aspx
- 模版详解(模版与宏) 转自:http://www.cppblog.com/zmllegtui/archive/2008/10/28/65316.html
- WCHAT、TCHAT、CString和char*的转换(转载于http://www.cppblog.com/zhangyq/archive/2008/08/21/59586.aspx)
- 工欲善其事,必先利其器——VC2005的常用快捷键(来自http://www.cppblog.com/corelito/archive/2008/10/17/64233.html)
- NLP常用工具 from:http://www.cppblog.com/baby-fly/archive/2010/10/08/129003.html
- VC调试总结 zz http://www.cppblog.com/kevinlynx/archive/2008/04/24/47998.html 博客。
- 二分查找学习札记转自http://www.cppblog.com/converse/archive/2009/10/05/97905.html
- http://www.cppblog.com/flyinghearts/archive/2012/02/28/166713.aspx#_Toc317966391
- tinyxml使用笔记与总结 http://www.cppblog.com/elva/archive/2008/04/24/47907.html
- 容斥原理(转载http://www.cppblog.com/vici/archive/2011/09/05/155103.html)
- 如何检查C++中的内存泄漏 (原文地址:http://www.cppblog.com/Lyt/archive/2009/03/22/77517.html)
- http://www.cppblog.com/twzheng/archive/2008/07/07/55563.html
- http://www.cppblog.com/weiym/archive/2012/06/07/177958.aspx