float、int、unsigned int数据与其在实际内存中表示的相互转换小程序
小程序来源
在嵌入式调试过程中,经常会收到一些数据,然而并不能直接读出数据所代表的的真实数值,基于这个困扰,编写了一个基于MFC对话框的float、int、unsigned int数据与其在实际内存中表示的相互转换小程序,附件带有源文件,欢迎小伙伴交流讨论。
程序功能
本程序可以实现内存中存储的4字节数据,转换成float、int、unsigned int数据,亦可查看float、int、unsigned int数据所对应的内存内容。
程序源码
为方便大家下载程序,可以点击底部链接下载源文件,运行环境为MFC。
//内存转化为FLOAT
void CDataTypeChangeDlg::OnBnClickedButton1()![在这里插入图片描述](https://img-blog.csdnimg.cn/20210113132831897.PNG#pic_center)
{
// TODO: 在此添加控件通知处理程序代码
CString sTemp = _T("");
std::string csTemp;
char cTemp[3];
byte bData[4];
unsigned int dDate = 0;
float fData = 0.0f;
//获取输入的内存数据
GetDlgItem(IDC_EDIT_BYTE_1)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[0] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据1长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_2)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[1] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据2长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_3)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[2] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据3长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_4)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[3] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据4输入错误"));
return;
}
//将内存数据存储为unsigned int
dDate = (unsigned int)bData[0] + (((unsigned int)bData[1]) << 8) + (((unsigned int)bData[2]) << 16) + (((unsigned int)bData[3]) << 24);
//取内存数据并转化成FLOAT类型
fData = *((float*)(&dDate));
//打印显示
sTemp.Format(_T("%.6f"),fData);
GetDlgItem(IDC_EDIT_FLOAT)->SetWindowText(sTemp);
}
//FLOAT转化为内存
void CDataTypeChangeDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
CString sTemp = _T("");
std::string csTemp;
char cTemp[20];
byte bData[4];
float fData = 0.0f;
unsigned int dData = 0;
//获取输入的FLOAT数据值
GetDlgItem(IDC_EDIT_FLOAT)->GetWindowText(sTemp);
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
fData = float(atof(cTemp));
//取数据地址内的值并保存
dData = *(unsigned int*)(&fData);
bData[0] = (byte(dData)) & 0xff;
bData[1] = (byte(dData >> 8)) & 0xff;
bData[2] = (byte(dData >> 16)) & 0xff;
bData[3] = (byte(dData >> 24)) & 0xff;
//显示内存数据
sTemp.Format(_T("%.2x"),bData[0]);
GetDlgItem(IDC_EDIT_BYTE_1)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[1]);
GetDlgItem(IDC_EDIT_BYTE_2)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[2]);
GetDlgItem(IDC_EDIT_BYTE_3)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[3]);
GetDlgItem(IDC_EDIT_BYTE_4)->SetWindowText(sTemp);
}
//内存转化为整型
void CDataTypeChangeDlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
if (sizeof(unsigned int) != 4)
{
MessageBox(_T("本系统整型数据不是占用4字节内存,无法转化!"));
return;
}
CString sTemp = _T("");
std::string csTemp;
char cTemp[3];
byte bData[4];
unsigned int dDate = 0;
int iData = 0;
//获取输入的内存数据
GetDlgItem(IDC_EDIT_BYTE_1)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[0] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据1长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_2)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[1] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据2长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_3)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[2] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据3长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_4)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[3] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据4输入错误"));
return;
}
//将内存数据存储为unsigned int
dDate = (unsigned int)bData[0] + (((unsigned int)bData[1]) << 8) + (((unsigned int)bData[2]) << 16) + (((unsigned int)bData[3]) << 24);
//取内存数据并转化成INT类型
iData = *((int*)(&dDate));
//打印显示
sTemp.Format(_T("%d"),iData);
GetDlgItem(IDC_EDIT_INT)->SetWindowText(sTemp);
}
//整型转内存
void CDataTypeChangeDlg::OnBnClickedButton4()
{
// TODO: 在此添加控件通知处理程序代码
if (sizeof(unsigned int) != 4)
{
MessageBox(_T("本系统整型数据不是占用4字节内存,无法转化!"));
return;
}
CString sTemp = _T("");
std::string csTemp;
char cTemp[20];
double fData = 0.0f;
int iData = 0;
unsigned int dData = 0;
byte bData[4];
//获取输入数据值
GetDlgItem(IDC_EDIT_INT)->GetWindowText(sTemp);
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
fData = atof(cTemp);
if (fData <= (pow(2.0f,31) - 1) && fData >= -pow(2.0f,31))
{
iData = atoi(cTemp);
}
else
{
MessageBox(_T("整型数据超过范围,请重新输入!"));
return;
}
//取数据地址内的值并保存
dData = *(unsigned int*)(&iData);
bData[0] = (byte(dData)) & 0xff;
bData[1] = (byte(dData >> 8)) & 0xff;
bData[2] = (byte(dData >> 16)) & 0xff;
bData[3] = (byte(dData >> 24)) & 0xff;
//显示内存数据
sTemp.Format(_T("%.2x"),bData[0]);
GetDlgItem(IDC_EDIT_BYTE_1)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[1]);
GetDlgItem(IDC_EDIT_BYTE_2)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[2]);
GetDlgItem(IDC_EDIT_BYTE_3)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[3]);
GetDlgItem(IDC_EDIT_BYTE_4)->SetWindowText(sTemp);
}
//内存转化为U整型
void CDataTypeChangeDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
if (sizeof(unsigned int) != 4)
{
MessageBox(_T("本系统无符号整型数据不是占用4字节内存,无法转化!"));
return;
}
CString sTemp = _T("");
std::string csTemp;
char cTemp[3];
byte bData[4];
unsigned int dDate = 0;
//获取输入的内存数据
GetDlgItem(IDC_EDIT_BYTE_1)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[0] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据1长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_2)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[1] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据2长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_3)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[2] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据3长度输入错误"));
return;
}
GetDlgItem(IDC_EDIT_BYTE_4)->GetWindowText(sTemp);
if (sTemp.GetLength() == 2)
{
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
bData[3] = GetHexCharToInt(cTemp[0]) * 16 + GetHexCharToInt(cTemp[1]);
}
else
{
MessageBox(_T("内存数据4输入错误"));
return;
}
//将内存数据存储为unsigned int
dDate = (unsigned int)bData[0] + (((unsigned int)bData[1]) << 8) + (((unsigned int)bData[2]) << 16) + (((unsigned int)bData[3]) << 24);
//打印显示
sTemp.Format(_T("%u"),dDate);
GetDlgItem(IDC_EDIT_UINT)->SetWindowText(sTemp);
}
//U整型转内存
void CDataTypeChangeDlg::OnBnClickedButton6()
{
// TODO: 在此添加控件通知处理程序代码
if (sizeof(unsigned int) != 4)
{
MessageBox(_T("本系统无符号整型数据不是占用4字节内存,无法转化!"));
return;
}
CString sTemp = _T("");
std::string csTemp;
char cTemp[20];
double fData = 0.0f;
unsigned int dData = 0;
byte bData[4];
//获取输入数据值
GetDlgItem(IDC_EDIT_UINT)->GetWindowText(sTemp);
csTemp = CW2A(sTemp);
strcpy_s(cTemp,csTemp.c_str());
fData = atof(cTemp);
if (fData <= (pow(2.0f,32) - 1) && fData >= 0)
{
dData = unsigned int(fData);
}
else
{
MessageBox(_T("无符号整型数据超过范围,请重新输入!"));
return;
}
//取数据地址内的值并保存
bData[0] = (byte(dData)) & 0xff;
bData[1] = (byte(dData >> 8)) & 0xff;
bData[2] = (byte(dData >> 16)) & 0xff;
bData[3] = (byte(dData >> 24)) & 0xff;
//显示内存数据
sTemp.Format(_T("%.2x"),bData[0]);
GetDlgItem(IDC_EDIT_BYTE_1)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[1]);
GetDlgItem(IDC_EDIT_BYTE_2)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[2]);
GetDlgItem(IDC_EDIT_BYTE_3)->SetWindowText(sTemp);
sTemp.Format(_T("%.2x"),bData[3]);
GetDlgItem(IDC_EDIT_BYTE_4)->SetWindowText(sTemp);
}
插入链接与图片
链接: [link](https://www.csdn.net/).
图片: