mfc十六进制显示 [英] mfc Hex display

查看:147
本文介绍了mfc十六进制显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mfc CFile :: read读取.bin文件.该文件包含十六进制值,例如03 07 09 da 56 44等.我只是在检查是否可以读取和显示这些值.现在,我将其中一个值存储在一个长度为char的char数组中,仅用于检查.发生的是,当我将值写入另一个文件时,该文件已正确写入.但是,当我想显示或转换它以便我使用和操纵它时,它不会发生,请尝试所有可能的转换类型.这是代码.

I want to read a .bin file using mfc CFile::read. The file contains hex values such as 03 07 09 da 56 44 etc . I was just checking if I can read and display these values . Now I store one value out of these in a char array of lenght one just for checking . What happens is when I write the value on to another file it is written properly . But when I want to display or convert it so that i cd use and manipulate it it doesnt happen, tried all possible types of conversion. here is the code .

char ret[1];
int y;
CString str,pet1[10];
cfile_object.Seek(5,CFile::begin);
cfile_object.Read(ret,1);
str.AppendChar(ret[0]);
y=atoi(ret);
itoa(y,pet1,10);
str=pet1;

AfxMessageBox(str);



:sigh:我尝试过strtol,_tcstol等,但无法获取值.它只将44显示为"D",以十六进制显示13,其余值(例如03 05等)显示为NULL,并以十六进制显示为0.

总而言之,我想将长度为1的Char数组中的值转换为其适当的十六进制值并将其存储.

请帮助,它在Char数组中,但是无法使用.谢谢.



:sigh: I have tried strtol, _tcstol etc but not able to get the values . It just displays 44 as ''D'' and 13 in hex, rest values such as 03 05 etc are shown as NULL and displayed as 0 in hex .

To summarise , I want to convert the values in the Char array of lenght one into its proper hex value and store it .

Pls help , it is there in the Char array but cant get it to use.Thanks in advance . Sorry for the trouble guys.

推荐答案

字符已经是十六进制"值,无需转换.十六进制只是在输出设备上显示值的一种方式.例如,十六进制值0x44代表整数值68或字符"D".但是,由您自行决定以正确的格式打印.因此,在上面的代码中,您应该使用格式字符串%X"将您的字符显示为十六进制值,或者使用%d"将其显示为整数,或者当然将%c"显示为字符.
A character is already a ''hex'' value, no conversion is necessary. Hexadecimal is merely a way of displaying a value on an output device. For example the hex value 0x44 represents the integer value 68 or the character ''D''. However it is up to you to print it in the correct format. Thus in your code above you should use a format string of "%X" to display your character as a hex value, or "%d" to display it as an integer, or, of course, "%c" as a character.


您所说的正确.如果我执行%X",我将得到13的结果(我之前做过),它是D的十六进制,但我希望结果是44,这是该位置的实际值,所以我该怎么办.

例如,如果我创建一个新文件并在那儿写D,然后在用CFile读取时,我将得到输出为D和16(十六进制),现在如果在我的文件中而不是D(如果我用十六进制写44),我将得到D作为display和13作为其十六进制值,而shd则为44作为十六进制.这是因为出于某种原因,char数组在转换时会考虑显示的char,无论您做什么,而不是其背后的实际值.

也就是说,不是将44的字符值转换为十六进制,而是将D转换为13是没有用的.因此,对于诸如09,A的显示字符为NULL的值,它将采用NULL并将其转换为十六进制的0
这没用.

请帮助,这已经是一天了,我对此没有解决方案.
Well what u say is correct . If I do "%X" I wd get 13 as the result(I did it before) which is hex of D but I want 44 as result which is the actual value in that place , so what should I do .

For eg If I create a new file and write D over there and then while reading with CFile I will get output as D and 13 in hex , Now in he same file instead of D if I write 44 in hex ,I still get D as display and 13 as its hex value when it shd be 44 as hex . That is because for some reason the char array is considering the displayed char while conversion no matter what u do and not the actual value behind it .

That is , it is not taking 44''s character value and converting into hex but D and converting into 13 which is of no use . So for values such as 09 ,A whose display character is NULL it takes NULL and converts it into 0 in hex
which is of no use .

Pls help , Its been a Day and I have no solution for this.


如果您确实需要如上所述的十六进制值和格式设置选项的字符表示,请不要删减满足您的需求;您可以随时使用流行/荒谬的内容.

itoa(YourHex, buffer, 16)//its quick its dirty and it kind of works

我不得不大量读取奇数字节/位大小单位的二进制值.但是,我建议您是否要使用itoa拆分十六进制值,而不是直接进行转换. Itoa会将0x01变成1.凝视十六进制时,这在逻辑上没有什么用处,因为从逻辑上讲,我们希望将其视为01.

If you really need the character representation of the HEX value and formating options as described above don''t quite cut it for your needs; you can always use the ever popular/ridiculed.

itoa(YourHex, buffer, 16)//its quick its dirty and it kind of works

I''ve had to do alot of reading of binary values for odd byte/bit sized units. I do however recommend if you are going to use itoa that you split the hex value up rather than a straight conversion call. Itoa will turn 0x01 into 1 . When staring at hex that is less than helpful since logically we expect to see it as 01.

highChar = YourChar >> 4;<br />
lowChar = YourChar %0x10;



P.S.处理1字节的十六进制值时.我会用
阅读
未签名的字符



P.S. when dealing with hex values that are 1 byte. I would read in using

unsigned char


这篇关于mfc十六进制显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆