Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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: 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.
Posted
Updated 29-Jul-10 1:16am
v4
Comments
Emilio Garavaglia 30-Jul-10 9:58am    
Reason for my vote of 1
bad formulated ambiguous question
Richard MacCutchan 12-Aug-10 4:08am    
Thank you for you last comment below, I quote "Either u r dumb or just a jerk. Get lost". Perhaps if you actually took some of the advice given to you instead of resorting to abuse you would get a solution to your problem.
AbhayRane 12-Oct-11 9:57am    
just saw ur reply , logged in after a long time . The advice u gave did not work , and i specified that earlier . No matter how many times i tell u , you are not going to get it . I solved my problem and also posted the solution . I also made two tools for the game and its a success (I'm a modder) . So u can go and f*** urself
Richard MacCutchan 12-Oct-11 15:23pm    
Why on earth did you come back here just to be abusive? You have amply demonstrated the fact that you are an ignorant moron, you really don't need to keep repeating it.

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.
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Richard MacCutchan 29-Jul-10 11:15am    
This is not possible, if you display the hex value of the character 'D' you will get x44. I suspect you are taking the hex value xD and displaying as an integer which is 13. Please re-read my suggestions above, and also your introductory books on computing in general and C/C++ in particular.
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. when dealing with hex values that are 1 byte. I would read in using

unsigned char
 
Share this answer
 
Comments
Sauro Viti 29-Jul-10 11:11am    
To translate byte values to their hexadecimal representation you can use sprintf(buffer, "%02X", value); the format string "%02X" mean: format in hex using two digits and fill with zero on the left (then if value = 0x01, inside buffer you obtain "01")
Sorry friend , but that is not working IDK why . It is giving me 0 as output for hex values which does not have a display char .

Suppose there is a .bin file with hex values as u open and see in a hex editor.

How wd u go about and display atleast one of the hex value(say the very first one) in mfc .I only need that code , rest I will do everything. I just need the code for the correct value in the file to be displayed . I dont want to know what char it is displaying , I just want the correct hex values that it posseses to be displayed . Can anyone give me only that much code(only for anyone byte in the file). I will do the rest . Please , please . code for the first byte in the .bin file will also do . thanks .
 
Share this answer
 
Comments
Richard MacCutchan 29-Jul-10 11:41am    
I don't know how many times I have to say this, but please read my original answer. That is all you have to do, do NOT try converting your data into something else. Read a single byte and display it as hex with a "0x%.2X" format string; it works.
<code></code>unsigned char ret1[1],pet[3],pet1,k;
 BYTE ret[10]; 	
 int y,y1; 
	CString exp,exp1;
	CFileDialog FileDlg(TRUE, 0, 0, 0, _T("Raw Files (*.BIN)|*.BIN||"));
	  if( FileDlg.DoModal() == IDOK)
	{
		//f=FileDlg.GetFolderPath();
		//f1=FileDlg.GetFileName();
		//f.Append(r);
		//f.Append(f1);
		f=FileDlg.GetPathName();
		f1=FileDlg.GetFileName();
		cfile_object.Open(f,CFile::typeBinary|CFile::modeReadWrite);
	  }
      int rr;
	  CString str;
	  cfile_object.Seek(0,CFile::begin);
	  cfile_object.Read(ret,1);
	  str.AppendChar(ret[0]);
	  AfxMessageBox(str);//Display the wierd char as it is without converting
      	  
//Start converting into Hex values

             char c[10];
	     byte b;
		  
	  for(int y=0, x=0; y<1; ++y, ++x)  
	  {
		  b=((byte)(ret[0]>>4));

        c[x]=(char)(b>9 ? b+0x37 : b+0x30); //c[x] contains first number of the hex value.For eg 'D' of hex value 'D2' .
        f1=c[x];
	    b=((byte)(ret[0]&0xF));

        c[++x]=(char)(b>9 ? b+0x37 : b+0x30); // c[x] contains second number of the hex value. For eg '2' of hex value 'D2'
	   	  
		f1.AppendChar(c[x]);
		 
		AfxMessageBox(f1);
//loop ends and u have concatenated and displayed both number in one hex value ie 'D' and '2' together of the hex value 'D2' . So u got 'D2' which was actually there. Similarly do for others as well  
          
	  }
	  
	  cfile_object.Close();


Finally I got it . Here it is , I wrote it referring to a code on Visual C# because wrt MFC , i was not getting any help. There are a lot of unused variables, I forgot to flush them .

Thanks to all who answered my query . It did open up my concept a bit . Thanks guys . Y'all Rock . :) . :D .
 
Share this answer
 
v2
Comments
Richard MacCutchan 29-Jul-10 16:49pm    
Why do you need all this redundant code? I showed you how to do it above: read one byte into a variable and display it with printf/sprintf etc using the simple format "0x%.2X". You should really spend some time learning about how data is held in computer memory and why you do not need any conversion for this process.
Thats what I m trying to tell u Richard the % thing was not working .I know how data is stored and how to retrieve using % thing but try to do it with CFile :: Read(), it doesnot work , the data is already stored in char form in the char array .

I know when I was learning C++ I used to do this %s,%c,%d thing to convert then and there but it doesnt work over here otherwise I wdnt have gone this far.

The piece of code that SnowHow posted was very close but I cdnt get it to use without referring to a very good example in VC# .

U wd have to use this ret[0]>>4 for lowchar and ret[0]&0xF this for upper char of a byte like SnowHow said .

So Ur type of conversion is the first to come to my mind and i tried it Richard but doesnt work with CFile::Read . U have to convert it using the above code. I m not trying to offend u but that is the truth . Anyways I m grateful for ur help and effort.
 
Share this answer
 
Comments
Richard MacCutchan 30-Jul-10 5:29am    
Sorry but you are totally wrong. The code I posted was not a figment of my imagination, it was code that I compiled into a test program and ran to successful conclusion. Try this:

// no need to seek after open() cfile_object.Seek(0,CFile::begin);
cfile_object.Read(ret,1);
printf("contents of ret: 0x%.2X", ret[0]);

AbhayRane 30-Jul-10 8:20am    
HEy Richard u dont understand what I need, I dont need to just print the thing , I also need to store it in a variable so that i could add those hex value to some other hex values. And that printf doent work in Dialog based Application or is there anyway to make it work . with printf nothing happens . How to display using printf in a dialog based application window.
Richard MacCutchan 30-Jul-10 9:46am    
Use sprintf to format it into a string, or CString::format if you are using MFC. And you do not need to store it so you can add it to other variables; you will not get the results you think. As I said before the variable that you have read into IS hex (or character, or integer) - I really get the feeling that you still have not grasped this basic concept.
AbhayRane 30-Jul-10 10:35am    
CString :: format gives the same result given by atoi() which I have used and its problems I have described in very first post . Anyways I am making this tool for a Game to increase the size of any file . The files sizes are encrypted and u cant import files with greater sizes unless u keep on increasing its offset memory till the last file in order which is stored as hex values in the bin file .

And Nevermind , I will use the >> and & method which is quite suiting my needs . So cheers bro .:) .
Richard MacCutchan 30-Jul-10 16:00pm    
Well I have no idea what this last comment is about, CString::format definitely does not give the same result as atoi(), they are totally different functions. Also this has nothing to do with increasing file sizes, and the last sentence above makes no sense whatsoever. I suspect that the language issue has meant that we have misunderstood what your problem is and are trying to offer solutions to a totally different issue; for which I apologise. I don't know what your first language is but perhaps it would help if you ask someone else to translate your question into English so that we can be of more help.
Quote:
"This has nothing to do with file sizes", Yeah in general , NO. But I told u this game stores the sizes of the files in a different file along with its memory offset. This method is not for all files only for the game files ,now u understand"


What game and what does this have to do with your original question of displaying values as HEX?

Quote:
"Secondly " >> method " means ret[0]>>4 method in the above code . So u got that now. :) ."


No, sorry but I have no idea what this sentence means

Quote:
"Have u used MFC and tried to Display the hex values in a file using CString :: Format ."


Yes I have and it works as I explained.

Quote:
"Logic is a different thing and Implementation is a different thing. U have to understand that there are these logical bugs and then there are these implementation bugs ."


Again, I'm not sure what you mean here.

Quote:
"U are talking logically that this wdnt happen or that wdnt happen . But u must try it to know if it really happens . thanks"

Similarly, I am tired of repeating myself that I have tried what I suggested to you and it works 100% of the time. Unfortunately I believe that you are looking for a solution to a different problem which you have not explained clearly to us. As I said before this may be down to your command of English which is why I suggested you perhaps get some assistance with your translation.
 
Share this answer
 
Comments
AbhayRane 12-Aug-10 3:11am    
Is translation so important . I think interpretation is more important dude. U should be able to understand what i m saying by now. Or atleast think of the possible problem that wd be there related to hex. I gave u a simplified question , It cant get simpler than this. My command over the language might not be the best , I agree , but it is not so bad that people cant understand . Either u r dumb or just a jerk. Get lost .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900