如何将01001010 按位转换成一个字节啊?
如何将01001010 按位转换成一个字节啊?
如01001010 转换后为4A
问题点数:50、回复次数:11Top
1 楼holyeagle(一杯清茶)回复于 2004-08-04 19:49:19 得分 20
BYTE nIndex = 0;
CHAR chBuf[] = {"01001010"};
for (INT i = 0; i < 8; ++i)
{
if (chBuf[i] == '1')
{
nIndex<<1;
}
}
Top
2 楼flyelf(空谷清音)回复于 2004-08-04 19:49:45 得分 10
01001010是字符串吗?Top
3 楼wuangle(#C学鬼#)回复于 2004-08-04 19:59:53 得分 0
01001010是字符串吗?
-----
是字符串01001010...这只是一小部分
Top
4 楼lixiaosan(小三)回复于 2004-08-04 20:03:36 得分 20
CString BinaryToHex(CString strBinary)
{
int nLength = strBinary.GetLength();
CString str = strBinary;
//位数不是四的倍数时补齐
switch(nLength%4)
{
case 0:
break;
case 1:
strBinary.Format("%d%d%d%s",0,0,0,str);
break;
case 2:
strBinary.Format("%d%d%s",0,0,str);
break;
case 3:
strBinary.Format("%d%s",0,str);
break;
default:
return "";
break;
}
CString strHex,str1;
str1 = "";
nLength = strBinary.GetLength();
for(int i=1;i<=(nLength/4);i++)
{
//每四位二进制数转换为一十六进制数
str = strBinary.Left(4);
CString strDecimal = BinaryToDecimal(str);
int nDecimal = atoi(strDecimal.GetBuffer(0));
if(nDecimal<10)
str1.Format("%d",nDecimal);
else
{
char c = 'A' + (nDecimal-10);
str1.Format("%c",c);
}
strHex += str1;
strBinary = strBinary.Right(strBinary.GetLength()-str.GetLength());
}
return strHex;
}Top
5 楼holyeagle(一杯清茶)回复于 2004-08-04 20:05:02 得分 0
刚才代码有点错误,如果很长的字符串,可以用指针索引
BYTE nIndex = 0;
BYTE lpData = lpSrc;
for (INT i = 0; i < 8; ++i)
{
nIndex = nIndex<<1;
if ('1' == lpData [i])
{
nIndex += 1;
}
}
Top
6 楼wuangle(#C学鬼#)回复于 2004-08-04 22:17:39 得分 0
谢谢名位的帮忙
to :lixiaosan(小三) ( )
CString strDecimal = BinaryToDecimal(str);
是什么啊,我怎么在MSDN上找不到啊?Top
7 楼lixiaosan(小三)回复于 2004-08-04 22:33:39 得分 0
CString BinaryToDecimal(CString strBinary)//转换二进制为十进制
{
int nLenth = strBinary.GetLength();
char* Binary = new char[nLenth];
Binary = strBinary.GetBuffer(0);
int nDecimal = 0;
for(int i=0;i<nLenth;i++)
{
char h = Binary[nLenth-1-i];
char str[1];
str[0] = h;
int j = atoi(str);
for(int k=0;k<i;k++)
{
j=j*2;
}
nDecimal += j;
}
CString strDecimal;
strDecimal.Format("%d",nDecimal);
return strDecimal;
}Top
8 楼Varg(Varg)回复于 2004-08-04 23:57:30 得分 0
建议使用bitset!
Example
// bitset_to_ulong.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>
int main( )
{
using namespace std;
bitset<5> b1 ( 7 );
cout << "The ordered set of bits in the bitset<5> b1( 7 )"
<< "\n that was generated by the number 7 is: ( "
<< b1 << " )" << endl;
unsigned long int i;
i = b1.to_ulong( );
cout << "The integer returned from the bitset b1,"
<< "\n by the member function to_long( ), that"
<< "\n generated the bits as a base two number is: "
<< i << "." << endl;
}
Output
The ordered set of bits in the bitset<5> b1( 7 )
that was generated by the number 7 is: ( 00111 )
The integer returned from the bitset b1,
by the member function to_long( ), that
generated the bits as a base two number is: 7.
Top
9 楼wuangle(#C学鬼#)回复于 2004-08-05 08:48:56 得分 0
好像可以这样
for(i = 0;i<n;i++)
if(lpSrc[i]=='1')
{
lpDev[i/8] = 1<<(i%8)
}Top
10 楼jszj(老板说mis部不是赚钱的部门...)回复于 2004-08-05 08:56:27 得分 0
不知道楼主的问题是什么
8位的,转换成两个字节吗?那是说每4位你把它当作一个字节?但那个A是怎么来的呢?4又是怎么来的呢?
看来,4是你把前4位当作二进制转换来的,但那个A就全然猜不到是怎么来的了Top
11 楼DeautyFan(可可魔仙)回复于 2004-08-05 08:58:53 得分 0
先分段(4位一段),之后用atoi,就可以将每段分成 比如 1101 的数,而现在就简单了
1 1 0 1
因为 D=8+4+0+1Top




