百合文库
首页 > 网文

什么都没有

2023-11-13 来源:百合文库

什么都没有



汉字转换成二进制、十进制和十六进制显示
今天我的网友猎心锁跟我讨论汉字转十六进制的问题。二进制、十进制、十六进制这三个进制之间的转换就让我头疼,现在又加上了汉字,无疑是“雪上加霜”。我的大脑不堪重负,思绪混乱。下午到图书馆查阅资料,也没发现有价值的线索,以致最终都没有讨论出答案。
在我的网友“冷静啊冷静”的帮助下,终于在www.pudn.com上找到了相关的资料,下载下来试试,果然实现了我们想要的功能:将输入的汉字分别转换成了二进制、十进制和十六进制,程序运行效果如下图:

现将程序的主要代码分享如下:

什么都没有


void CUnicodeDlg::OnTranslate()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString PP;
PP=m_chinesecharacters;
if(m_chinesecharacters.IsEmpty())
{
AfxMessageBox("请输入汉字!");
return;
}
int len=PP.GetLength();

什么都没有


int unicode; //十进制转化为十六进制进用到的中间值
const int MAX_FILE_LENGTH=500;
WCHAR UnicodeFile[MAX_FILE_LENGTH];
MultiByteToWideChar( CP_ACP, 0, PP,-1,UnicodeFile,MAX_FILE_LENGTH);
PP="";
CString midd,temp="";//中间值
for(int loop=0;loop<len/2;loop )

什么都没有


{
unicode=UnicodeFile[loop];
m_binary=tentobinary(unicode);
midd.Format("%d",unicode);
temp.Format("%d",unicode);
m_unicodedec=m_unicodedec temp;
m_unicodedec=m_unicodedec " ";
midd.Format("%d",(unicode)/(16*16*16));

什么都没有


midd=tentosixteen(midd);
PP=PP midd;
unicode=unicode-(unicode)/(16*16*16)*16*16*16;
midd.Format("%d",(unicode)/(16*16));
midd=tentosixteen(midd);
PP=PP midd;
unicode=unicode-(unicode)/(16*16)*16*16;
midd.Format("%d",(unicode)/16);

什么都没有


midd=tentosixteen(midd);
PP=PP midd;
unicode=unicode-unicode/16*16;
midd.Format("%d",unicode);
midd=tentosixteen(midd);
PP=PP midd; //这是最后结果
m_unicodehex=PP;
UpdateData(false);
}
}
CString CUnicodeDlg::tentosixteen(CString ten)

什么都没有


{
if(ten=="10")
ten="A";
if(ten=="11")
ten="B";
if(ten=="12")
ten="C";
if(ten=="13")
ten="D";
if(ten=="14")
ten="E";
if(ten=="15")
ten="F";

什么都没有


return ten;
}
CString CUnicodeDlg::tentobinary(int iNum)
{
int i; /*循环变量*/
char a[17]; /*输出的进制数*/
memset(a,0,17);
a[16]='/0';
printf("please input the number(>0):");
scanf("%d",&iNum);
for (i=15;i>=0;i--)

什么都没有


{
if(iNum==0 )
{
for(;i>=0;i--)
{
a[i]='0';
}
break;
}
if(iNum%2 == 0 )
{/*如果进制数被整除*/
iNum=iNum/2;
a[i] ='0'; /*相应进制数位置*/
}
else
{
iNum=(iNum-1)/2;
汉字转二进制代码
void CUnicodeDlg::OnTranslate()

什么都没有


{
// TODO: Add your control notification handler code here
UpdateData(true);
CString PP;
PP=m_chinesecharacters;
if(m_chinesecharacters.IsEmpty())
{
AfxMessageBox("请输入汉字!");
return;
}
int len=PP.GetLength();
int unicode; //十进制转化为十六进制进用到的中间值

什么都没有


const int MAX_FILE_LENGTH=500;
WCHAR UnicodeFile[MAX_FILE_LENGTH];
MultiByteToWideChar( CP_ACP, 0, PP,-1,UnicodeFile,MAX_FILE_LENGTH);
PP="";
CString midd,temp="";//中间值
for(int loop=0;loop<len/2;loop )
{
unicode=UnicodeFile[loop];

什么都没有


m_binary=tentobinary(unicode);
midd.Format("%d",unicode);
temp.Format("%d",unicode);
m_unicodedec=m_unicodedec temp;
m_unicodedec=m_unicodedec " ";
midd.Format("%d",(unicode)/(16*16*16));
midd=tentosixteen(midd);

什么都没有


PP=PP midd;
unicode=unicode-(unicode)/(16*16*16)*16*16*16;
midd.Format("%d",(unicode)/(16*16));
midd=tentosixteen(midd);
PP=PP midd;
unicode=unicode-(unicode)/(16*16)*16*16;
midd.Format("%d",(unicode)/16);
midd=tentosixteen(midd);

什么都没有


PP=PP midd;
unicode=unicode-unicode/16*16;
midd.Format("%d",unicode);
midd=tentosixteen(midd);
PP=PP midd; //这是最后结果
m_unicodehex=PP;
UpdateData(false);
}
}
CString CUnicodeDlg::tentosixteen(CString ten)
{
if(ten=="10")

什么都没有


ten="A";
if(ten=="11")
ten="B";
if(ten=="12")
ten="C";
if(ten=="13")
ten="D";
if(ten=="14")
ten="E";
if(ten=="15")
ten="F";
return ten;
}

什么都没有


CString CUnicodeDlg::tentobinary(int iNum)
{
int i; /*循环变量*/
char a[17]; /*输出的进制数*/
memset(a,0,17);
a[16]='/0';
printf("please input the number(>0):");
scanf("%d",&iNum);
for (i=15;i>=0;i--)
{

什么都没有


if(iNum==0 )
{
for(;i>=0;i--)
{
a[i]='0';
}
break;
}
if(iNum%2 == 0 )
{/*如果进制数被整除*/
iNum=iNum/2;
a[i] ='0'; /*相应进制数位置*/
}
else
{
iNum=(iNum-1)/2;
a[i]= '1'; /*否则置 */

什么都没有


}

return (CString)a;
}
a[i]= '1'; /*否则置 */
}

return (CString)a;
}


猜你喜欢