背景:将两个字节 byte1和 byte2中的二进制位重新组合, byte1的低3位加上 byte2的高5位,然后形成一个新的2进制字符串str5,共8位,然后转化为整数,目前的写法是:
int byte1=0x59,byte2=0x0c,i;
CString str,str1,str3,str4,str5,byte21_11to4;
char strtemp[8],strtemp2[8];
itoa(byte1,strtemp,2);
str1.Format("%s",strtemp);
str=(CString("00000000"+str1)).Right(8); itoa(byte2,strtemp2,2);
str3.Format("%s",strtemp2);
str4=(CString("00000000"+str3)).Right(8);
byte21_11to4=str4.Right(3)+str.Left(5);
i=TurnBinary(byte21_11to4);
str5.Format("%d",i);//str5就是最终的结果int TurnBinary(CString str)
{
int length = str.GetLength();
int value = 1;
int result = 0;
if(length>32)
{
AfxMessageBox("超过32字节!");
return 0;
}
else if(length == 0)
{
return 0;
}
else
{
for(int i = length-1;i>=0;i--)
{
if(str.Mid(i,1)=="1")
result+=value;
value*=2;
}
}
return result;}请大家看一下,有没有更简单的写法?谢谢。
bytec
int byte1=0x59,byte2=0x0c,i;
CString str,str1,str3,str4,str5,byte21_11to4;
char strtemp[8],strtemp2[8];
itoa(byte1,strtemp,2);
str1.Format("%s",strtemp);
str=(CString("00000000"+str1)).Right(8); itoa(byte2,strtemp2,2);
str3.Format("%s",strtemp2);
str4=(CString("00000000"+str3)).Right(8);
byte21_11to4=str4.Right(3)+str.Left(5);
i=TurnBinary(byte21_11to4);
str5.Format("%d",i);//str5就是最终的结果int TurnBinary(CString str)
{
int length = str.GetLength();
int value = 1;
int result = 0;
if(length>32)
{
AfxMessageBox("超过32字节!");
return 0;
}
else if(length == 0)
{
return 0;
}
else
{
for(int i = length-1;i>=0;i--)
{
if(str.Mid(i,1)=="1")
result+=value;
value*=2;
}
}
return result;}请大家看一下,有没有更简单的写法?谢谢。
bytec
解决方案 »
- 如何安排打印顺序,,,打印顺序真能安排吗??????
- 急!用CStdioFile读UNICODE文件用ReadString的问题
- 有没有哪位兄弟懂foxpro的,用foxpro调用VC的DLL
- dll Attach to process 添加到inetinfo, 老是报dll找不到
- 那里有类似QQ聊天软件的源代码下载?
- 各位大虾,小弟请教关于网络数据压缩传输的问题?
- 请问:能不能根据toolbar的handle得到它里面的combo box的handle
- 关于服务进程的问题--服务进程弹出的错消息怎么获取??
- 使用HORZSIZE 和 VERTSIZE参数调用GetDeviceCaps(),逻辑宽度和逻辑高度的疑问
- 请问:<<windows网络编程>>(机械工业出版社出版)这本书南京有卖吗?
- 请教比较精确控制SOCKET发送速率的方法
- VC 多文档里能不能共用一个菜单?
{
int length = str.GetLength();
int value = 1;
int result = 0;
if(length>32)
{
AfxMessageBox("超过32字节!");
return 0;
}
else if(length == 0)
{
return 0;
}
else
{
for(int i = length-1;i>=0;i--)
{
if(str.Mid(i,1)=="1")
result+=value;
value*=2;
}
}
return result;
}