字符串转化成16进制!!谢谢各位大哥了!

y1982_0 2005-05-25 02:26:02
读一个文件后缀为.ilg,把里面的字符串欲全部转化成16进制显示出来,有代码的大哥给贴以下,不胜感激,已经两天了!我还没有弄出来呢!快疯了
...全文
210 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kyh1234567 2005-07-08
  • 打赏
  • 举报
回复
首先你可以定义一个一位转换函数TransDigit(char ch)把一位16进制字符值转换为整数,函数大致可以这样实现:
if (ch >= 'A' && ch <= 'F') i = 0x0A + ch - 'A';
else if (ch >= '0' && ch <= '9') i = ch - '0';
else i = 0;
return i;

然后,进行整体转换,以“5E”为例:
n = TransDigit('5') * 16 + TransDigit('E');

说明一下,如果有k位16进制数则转换公式为:
n = TransDigit(Dig1) * pow(16, k - 1) + TransDigit(Dig2) * pow(16, k - 2) + ... + TransDigit(Digk) * pow(16, 0);
用循环实现。

这里只是一个思路。:)
flice 2005-05-30
  • 打赏
  • 举报
回复
晚上回去给你完整的程序,现在不方便
flice 2005-05-30
  • 打赏
  • 举报
回复
http://www.qinhuan.com/fly/ue.rar
y1982_0 2005-05-28
  • 打赏
  • 举报
回复
顶顶!看看还有希望吗?楼上的兄弟,方法不太好用阿!
flice 2005-05-25
  • 打赏
  • 举报
回复
你不是吧,随便定义就可以了啊,就在前面定义就ok了啊
CFile m_pFile;
y1982_0 2005-05-25
  • 打赏
  • 举报
回复
m_pFile在哪里定义的啊,
flice 2005-05-25
  • 打赏
  • 举报
回复
FF D8 FF E0 00 10 4A 46 49 46 00 ............ 10 12 1F _3E E8 42 10 7F 8A 26 7E 91 86 27 _DB 43 7E 9C 10 84 25 E9 EC FE 0A _AC D3 F4 B7 74 21 09 0E 67 DF 9A _A9 FC FF 00 82 10 84 A3 3E C5 10 _7D 2E 84 20 E6 81 91 E2 A3 E9 FF _42 5E EF 72 FF D9
////////////
我打开的是jpg,看到那个“_”我故意添加的,你可以去掉,上面就是image,dat里面显示的啊,你说的20我这里就是空格,如果你想去掉空格,那就修改这个地方
////
for (int nIndex = 0; nIndex < nReturned; nIndex++)
{
if (nIndex == 0)
strTemp.Format(_T("%2.2X"), pszBuffer[nIndex]);
else if (nIndex % 16 == 0)
strTemp.Format(_T("=%2.2X"), pszBuffer[nIndex]);
else if (nIndex % 8 == 0)
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);
else
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);

这个里面的就是控制其显示的效果的
Format(_T("%2.2X") 就ok了
////
y1982_0 2005-05-25
  • 打赏
  • 举报
回复
能显示出来吧?放到一个编辑框里,我还准备进行下一步操作呢,准备把里面是20的全部去掉,因为那个20时空格,你的这个可以吗?大侠
flice 2005-05-25
  • 打赏
  • 举报
回复
就像UE打开一样,c:\image.dat就是c:\image.jpg的二进制打开后的内容

确实有点乱....
flice 2005-05-25
  • 打赏
  • 举报
回复
就像UE一样,打开c:\\image.jpg,数据都在c:\\image.dat里面
确实有点乱。。。
flice 2005-05-25
  • 打赏
  • 举报
回复
大致的意思是: 用二进制模式打开文件,然后读取->显示,以前做的代码(也是参考了别人的),你自己看看,我直接从我的工程里面copy出来的(我测试做的,有点乱,哈哈):
/////////////////////////////////////////////////////////////
CFile myFile;
CFileException fileException;

if ( !myFile.Open( "c:\\image.dat", CFile::modeCreate |
CFile::modeReadWrite, &fileException ) )
{
TRACE( "Can't open file %s, error = %u\n",
"c:\\image.dat", fileException.m_cause );
}

try
{
m_pFile = new CFile("c:\\image.jpg", CFile::modeRead | CFile::typeBinary);
}
catch (CFileException* e)
{
CString strError;

strError.Format(_T("Couldn't open file: %d"),
_sys_errlist[e->m_lOsError]);
AfxMessageBox(strError);
// return FALSE;
}
CString imgSizes;
int imgSize;
int endLine;
imgSize=m_pFile->GetLength();
endLine=imgSize/16+1;
imgSizes.Format("%d",endLine);

BYTE t=0x2c525041;
CString f;
f.Format("%c",t);
AfxMessageBox(f);
AfxMessageBox(imgSizes);
if (m_pFile != NULL)
{

int nLine;
for (nLine = 0;nLine<endLine+100 ; nLine++)
{
if (!ReadLine(strRender, 16, nLine*16))
{
break;
}
else
{
//AfxMessageBox(strRender);
myFile.Write( strRender, /*76*//*49*/33);
//myFile.Seek( 0, CFile::end );
CString tmp="_";//(CString)char(10)+(CString)char(13);
myFile.Write(tmp, 1);
}

}
}

m_pFile->Close();
delete m_pFile;

}

::ReadLine(CString& strLine, int nLength, LONG lOffset = -1L)
{
LONG lPosition;

if (lOffset != -1L)
lPosition = m_pFile->Seek(lOffset, CFile::begin);
else
lPosition = m_pFile->GetPosition();

if (lPosition == -1L)
{
TRACE2("CHexViewDoc::ReadLine returns FALSE Seek(%8.81X, %8.81X)\n",
lOffset, lPosition);
return FALSE;
}

BYTE* pszBuffer = new BYTE[nLength];
int nReturned = m_pFile->Read(pszBuffer, nLength);

if (nReturned <= 0)
{
TRACE2("CHexViewDoc::ReadLine returns FALSE Read(%d, %d)\n",
nLength, nReturned);
delete pszBuffer;
return FALSE;
}

CString strTemp;
CString strCharsIn;

//strTemp.Format(_T("%8.8lX - "), lPosition);
strLine = strTemp;

for (int nIndex = 0; nIndex < nReturned; nIndex++)
{
if (nIndex == 0)
strTemp.Format(_T("%2.2X"), pszBuffer[nIndex]);
else if (nIndex % 16 == 0)
strTemp.Format(_T("=%2.2X"), pszBuffer[nIndex]);
else if (nIndex % 8 == 0)
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);
else
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);

if (_istprint(pszBuffer[nIndex]))
strCharsIn += pszBuffer[nIndex];
else
strCharsIn += _T('.');
strLine += strTemp;
}
if (nReturned < nLength)
{
CString strPadding(_T(' '), 3*(nLength-nReturned));
strLine += strPadding;
}
strLine += _T(" ");
strLine += strCharsIn;

delete pszBuffer;
return TRUE;
}

////////////////////////////////////////////////////////////
y1982_0 2005-05-25
  • 打赏
  • 举报
回复
顶顶看,有没有好心的大侠啊?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧