Hex editing GBA in C++ - c++

I am Programming on Mac OS X 10.6 with XCode. Im trying to make a Programm that "decrypts" a Pokemon GBA game so that you can read the dialogs of the game with any Text Editor.
I already got a programm finished witch does that, so I can read all Text Data.
My problem is, that I need to change the clear Text back to Hex after editing it, but I have no Idea what im making wrong.
string PathDE;
string PathGBA;
string Zeichen;
int kontrolle = 0;
int current = 0;
char buffer1[3] = "00";
char buffer2[3] = "a1";
...
schreiben.open(PathGBA.c_str(), ios::out | ios::binary); //writes out the edited hex
lesen.open(PathDE.c_str(), ios::in); //reads in the plain text
while(current <= kontrolle){
lesen.read(reinterpret_cast<char*>(&a), 1);
converter << a;
converter >> Zeichen;
if(Zeichen == "_"){
schreiben.write(buffer1,3);
}
else if(Zeichen == "0"){
schreiben.write(buffer2,3);
}
...
I Tryed that, but when I open the result in a Hex Editor, the ASCII code is only a8.a8.a8.a8... and so on till eof, and the Hex code is 61 38 00 ... till eof.
I also tryed other methods, but all with the same result, I cant write the decoded GBA as Hex so I can play it with edited Text.

You will want to convert your string into a byte[] with the correct encoding.
Here is the Gen3 text table for Pokemon
http://datacrystal.romhacking.net/wiki/Pok%C3%A9mon_FireRed_-_TBL
Once converted correctly you will simply just insert it into your rom.
Sorry if this doesnt help much but I only have made c# Pokemon tools, not c++.
And btw pokemon doesnt use a ascii hex encryption it uses a form of utf8.

Related

why I cant read a file to an integer vector?

well! I have a text file including some integer values and non-integers like character strings and white spaces so I want only to read integers values so I used a vector of integers but when I read the file the opining is ok but it seems the first input fails thus breaks the loop!!!
here is my main example:
ifstream in("file.txt");
if(in.fail())
cout << "opening failed!" << endl;
//opening is fine!
int value;
vector<int> v;
while(in >> value) // the problem here; it fails why?
{
cout << "ok"; // not printed
v.push_back(value);
}
cout << v.size() << endl; // 0??!!
this is the content of file.txt:
32 43 24 32
15 23
57
77 81
if I make a vector of chars it's ok but I want only to use one of integers
*** I already used a code like this and worked fine but now I don't know what happened??!!! it's really annoting
any help, comment, tip is welcome and appreciated
This line:
while(in >> value)
says while I can read integers...
But in the post this may not be true - you are not handling this case.
Either read stuff that is not integers and handle it. Or just read strings and then decide what to do.
In addition
cout << "ok"; // not printed
is because it is buffered.
Do this
cout << "ok" << flush; // printed
excuse me first for annoying you with nonsense question. finally I managed to discover the error:
in my main folder of project I unintentionally created a winrar file input.rar then I didn't remove it but rename it to input.txt it's ok I opened it manually and removed some unreadable characters. then I put inside it the content above of integers then my c++ application succeeds in opening it but can't read it.
*now I removed it input.txt which was input.rar and created a new document text input.txt and now everything is good!!!
thank you for your collaboration. and this post may help someone else.
* don't create rar file or other formats then rename them to be text files and try to read them via your c++ fstream because it'll fail in fact it'll produce an error-prone which looks impossible to solve

Garbage chars at the beginning of file

I'm reading a file, character by character using:
while(1)
{
char c ='\0';
c = infile.get();
cout << c << endl;
}
but I have a specific file where this code reads 3 (garbage = strange) characters before the actual data in my file (and only on the beginning of the file).
I've tried to open this file with some text editors (notepad and notepad++) but it seems right = no strange characters before my data...
Any idea why this strange chars are being read and how can I avoid it?
It is Byte Order Mark sequence with the hexadecimal representation of EF BB BF or .
more details here.

Binary file not holding data properly

Im currently trying to replace a text based file in my application with a binary one. Im just doing some early tests so the code isn't exactly safe but I'm having problems with the data.
When trying to read out the data it gets about half way before it starts coming back with incorrect results.
Im creating the file in c++ and my client application is c#. I think the problem is in my c++ (which I haven't used very much)
Where the problem is at the moment is I have a vector of a struct that is called DoubleVector3 which consists of 3 doubles
struct DoubleVector3 {
double x, y, z;
DoubleVector3(std::string line);
};
Im currently writing the variables individually to the file
void ObjElement::WriteToFile(std::string file) {
std::ofstream fileStream;
fileStream.open(file); //, ios::out | ios::binary);
// ^^problem was this line. it should be
// fileStream.open(file, std::ios_base::out | std::ios_base::binary);
fileStream << this->name << '\0';
fileStream << this->materialName << '\0';
int size = this->vertices.size();
fileStream.write((char*)&size,sizeof(size));
//i have another int written here
for (int i=0; i<this->vertices.size(); i++) {
fileStream.write((char*)&this->vertices[i].x, 8);
fileStream.write((char*)&this->vertices[i].y, 8);
fileStream.write((char*)&this->vertices[i].z, 8);
}
fileStream.close();
}
When I read the file in c# the first 6 sets of 3 doubles are all correct but then I start getting 0s and minus infinities
Am I doing anything obviously wrong in my WriteToFile code?
I have the file uploaded on mega if anyone needs to look at it
https://mega.co.nz/#!XEpHTSYR!87ihtCfnGXJJNn13iE6GIpeRhlhbabQHFfN88kr_BAk
(im writing the name and material in first then the number of vertices before the actual list of vertices)
Small side question - Should I delimit these doubles or just add them in one after the other?
To store binary data in a stream, you must add std::ios_base::binary to the stream's flags when opening it. Without this, the stream is opened in text mode and line-ending conversions can happen.
On Windows, line-ending conversions mean inserting a byte 0x0D (ASCII for carriage-return) before each 0x0A byte (ASCII for line-feed). Needless to say, this corrupts binary data.

Scanning a file in binary mode and interpret the hex characters as actual numbers in C++

I know this title might sound confusing. I have a simple question which I haven't been able to solve yet.
Lets imagine I have a file, opening it with an Hex Editor shows it has two characters inside, say 0x1B and 0x00 (obviously unprintable). I'd like to take that as 1B00 in HEX, which is 6912 in DEC, as opposed to directly converting the characters which would be wrong and is what all other questions I saw asked. Well, thats the task I want to do here. Seems simple, but everything I've tried just does it wrong! Even though I am obviously opening the file in binary mode.
I have only managed to read the characters individually, and mess around a bit, but never do what I actually want, which is as simple as taking those 2 hex characters, interpreting them as an Hex Number, and then convert it to Decimal.
Sorry for any unclear idea, Im not a native speaker. Any help will be appreciated, Im sure you'll think this was quite a noobish question :P
EDIT: Sorry, apparently I didn't explain myself properly. I know this might seem abstract, but it is a really concrete little thing which I have struggled to get solved, yet I haven't been able. Maybe I can ask it another way:
How can I scan a character in binary mode, lets say 0x1B, and convert that to actual 1B characters. Just that.
Sounds like you want to read the file as raw data, and then display it on the screen in decimal? Super easy!
int main() {
std::ifstream myfile("filename.data", std::ofstream::binary);
uint16_t number;
char* buffer = (char*)(&number);
while(myfile.read(buffer, sizeof(number))) {
std::cout << number << ' ';
}
}
The reason it's so easy is that there's no hexidecimal involved. The file is saved as a series of bytes, each byte holds one of 256 values. They aren't hex, they're just a series of values. If you read two bytes into the uint16_t, that is the easiest way to interpret two bytes as a single unsigned 2 byte value. And streaming out a uint16_t will, by default, display that value in decimal. There's no hexidecimal involved. The hexidecimal you saw in the hex editor was because a hex editor interprets the bytes as hex values.
If all you want to do is print a number in hexadecimal form, use std::hex
int i = 0x1B;
std::cout << std::hex << i << std::endl;
std::ifstream infile("test.bin", std::ofstream::binary);
while (true)
{
char c1 = ifs.get();
if (!infile.good())
{
break;
}
char c2 = ifs.get();
if (!infile.good())
{
break;
}
int num = (int)c1 |((int)c2 << 8);
// if you need the oppisite order then
// int num = (int)c2 &((int)c1 << 8);
cout << num;
}

(C++) Write string of hex to a file

This is driving me insane. I'm a beginner/intermediate C++er and I need to do something that seems simple. I have a string with A LOT of hex characters in it. They were inputted from a txt file. The string looks like this
07FF3901FF030302FF3f0007FF3901FF030302FF3f00.... etc for a while
How can I easily write these hex values into a .dat file? Everytime I try, it writes it as text, not hex values. I already tried writing a for loop to insert "\x" every byte but it still is written as text.
Any help would be appreciated :)
Note: Obviously, if I can even do this, then I don't know that much about c++ so try not to use things WAY over my head. Or at least explain it a bit. Pweeeez:)
You should be clear about the difference of char(ascii) and hex values.
Assume in x.txt:
ascii reads as: "FE"
In binary ,x.txt is "0x4645(0100 0110 0100 0101)".In ascii, 'F'=0x46,'E'=0x45.
Notice everything is computer is stored in binary code.
You want to get x.dat:
the binary code of x.dat is "0xFE(1111 1110)"
So, you should tranfer the ascii text into the proper hex values then write it into the x.dat.
The sample code:
#include<iostream>
#include<cstdio>
using namespace std;
char s[]="FE";
char r;
int cal(char c)// cal the coresponding value in hex of ascii char c
{
if (c<='9'&&c>='0') return c-'0';
if (c<='f'&&c>='a') return c-'a'+10;
if (c<='F'&&c>='A') return c-'A'+10;
}
void print2(char c)//print the binary code of char c
{
for(int i=7;i>=0;i--)
if ((1<<i)&c) cout << 1;
else cout << 0;
}
int main()
{
freopen("x.dat","w",stdout);// every thing you output to stdout will be outout to x.dat.
r=cal(s[0])*16+cal(s[1]);
//print2(r);the binary code of r is "1111 1110"
cout << r;//Then you can open the x.dat with any hex editor, you can see it is "0xFE" in binary
freopen("CON","w",stdout); // back to normal
cout << 1;// you can see '1' in the stdout.
}