| View previous topic :: View next topic |
| Author |
Message |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Mon Jul 14, 2008 9:43 pm Post subject: [C++] 1/2 Working hex editor |
|
|
I made a hex editor awhile ago, and it was shit. A few min ago I almost completely re-wrote it, using none of its old code. I fixed a problem with ASCII, but my the part that displays the hex doesn't work right. Every now and then it displays abunch of F's before a byte like this:
| Code: | | 4D 5A FFFFFF90 00 03 00 00 00 04 00 00 00 FFFFFFFF FFFFFFFF 00 00 FFFFFFB8 00 00 |
It should look like this:
| Code: | | 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 |
Here is a portion of my source code, which should compile (name hex.cpp/hex.exe):
| Code: | #include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int main() {
unsigned long int i = 0;
cout.unsetf(ios::dec);
cout.setf(ios::uppercase | /*ios::showbase |*/ ios::hex);
ifstream ino("hex.exe", ios::in | ios::binary);
int count = 0;
char c[999999];
while(!ino.eof()){
ino.get(c[i]);
if (c[i]==0)cout << hex << "00 ";
else if (c[i]==1)cout << "01 ";
else if (c[i]==2)cout << "02 ";
else if (c[i]==3)cout << "03 ";
else if (c[i]==4)cout << "04 ";
else if (c[i]==5)cout << "05 ";
else if (c[i]==6)cout << "06 ";
else if (c[i]==7)cout << "07 ";
else if (c[i]==8)cout << "08 ";
else if (c[i]==9)cout << "09 ";
else if (c[i]==10)cout << "0A ";
else if (c[i]==11)cout << "0B ";
else if (c[i]==12)cout << "0C ";
else if (c[i]==13)cout << "0D ";
else if (c[i]==14)cout << "0E ";
else if (c[i]==15)cout << "0F ";
else cout << (DWORD)c[i] << " ";
i++;
Sleep(25);
}
return 0;
} |
And I attached the program and source, which will open itself and output the hex.
PLZ HELP!  |
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Mon Jul 14, 2008 9:57 pm Post subject: |
|
|
Didn't look at the source, but when displaying each byte do this:
| Code: |
printf("%0.2hX ", ( theByte & 0xFF ) );
|
EDIT:
Looked at the code you put in your post, :S, please don't do that it hurts my eyes and brain XD No need to match each decimal and then display it as text, using printf (same as above) you can easily display it as hex. |
|
| Back to top |
|
 |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Mon Jul 14, 2008 9:59 pm Post subject: |
|
|
kk, and the source is what I posted. But it also includes the program.
EDIT:
HOLY SHIT! It works!
Maybe I should learn more C....  |
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Mon Jul 14, 2008 10:40 pm Post subject: |
|
|
Hehe, now for your next challenge. Make it display 16 bytes in hex and those 16 bytes in ASCII per line.. It's a bit of a brain teaser
i.e
| Code: |
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 AAAAAAAAAAAAAAAA
42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 BBBBBBBBBBBBBBBB
...
|
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25982 Location: The netherlands
|
Posted: Tue Jul 15, 2008 1:16 am Post subject: |
|
|
Hint, do a check if the value is bigger or equal to 32
Lower contain 'control' characters, like line break, delete, cursorpos left/right/up/down, backspace, etc...
You really do NOT want to display them. I recommend just writing a . or a empty space in those cases _________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Tue Jul 15, 2008 1:39 am Post subject: |
|
|
| x0r wrote: | | Dark Byte wrote: | Hint, do a check if the value is bigger or equal to 32
Lower contain 'control' characters, like line break, delete, cursorpos left/right/up/down, backspace, etc...
You really do NOT want to display them. I recommend just writing a . or a empty space in those cases |
IsCharAlpha  |
if ( theByte >= 0x20 )
{
...
}
works fine too
But i suppose, to each his own. |
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Tue Jul 15, 2008 3:33 am Post subject: |
|
|
| Use IsCharAlphaNumeric to check if the character is either numerical or alphabetical. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jul 15, 2008 4:43 am Post subject: |
|
|
Back to the original problem :p If you're going to do it the C++ way (or something): | Code: | std::cout.flags( std::ios_base::hex | std::ios_base::uppercase );
int i = file.get();
while( !file.eof() ) {
std::cout << std::setw(2) << std::setfill('0') << i << " ";
i = file.get();
} |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Tue Jul 15, 2008 7:07 am Post subject: |
|
|
| Code: |
BYTE b;
//...
cout << hex << b << endl;
|
Came across the "hex" modifier recently, figured you might like it. _________________
|
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Tue Jul 15, 2008 10:21 am Post subject: |
|
|
| x0r wrote: | | jackyyll wrote: | if ( theByte >= 0x20 )
{
...
}
works fine too
But i suppose, to each his own. |
That isn't UNICODE compliant. |
Hmm, suppose you are right there  |
|
| Back to top |
|
 |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Tue Jul 15, 2008 11:15 am Post subject: |
|
|
| Ik how to do IO and stuff, but I was just getting some wierd results. Ty for all your help. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
|
| Back to top |
|
 |
|