News:

11 March 2016 - Forum Rules

Main Menu

Help- command line hex edit tool

Started by DMCP2143, March 14, 2019, 09:42:01 PM

Previous topic - Next topic

DMCP2143

I want to make a command line hex edit tool.
program.exe file.name hexAddress newByte

This works.
#include <stdio.h>

int main ()
{   
    FILE *f = fopen( "Zelda3.sfc", "r+b" );
    fseek( f, 0x36D33, SEEK_SET );
    unsigned char newByte = 0x12;
    fwrite( &newByte, sizeof( newByte ), 1, f );
    fclose( f );
}


But arguments get me stumped. Best I have is this which compiles but does not work.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])  // program.exe file.name hexAddress newByte
{
    FILE *f = fopen( "argv[1]", "r+b" );
    int hexAddress = atoi( argv[2] );
    fseek( f, hexAddress, SEEK_SET );
    unsigned char newByte = atoi( argv[3] );
    fwrite( &newByte, sizeof( newByte ), 1, f );
    fclose( f );
}


Is this the place to ask for help? I want to make a bat file with my hex changes for notes and easy editing, maybe some toggle-able choices...

KingMike

One problem I immediately see is that you define "int".
I think it is the compiler which decides what an unspecified int size is.
But that could well be taken as a short signed int, which means you'd only be able to access bytes up to the first 32KB. (signed short int has a range -32k to +32k)

You want an unsigned long int (range 0 to 4GB).
"My watch says 30 chickens" Google, 2018

mziab

#2
Another obvious mistake is:
FILE *f = fopen( "argv[1]", "r+b" );

Putting argv[1] in quotes makes it open a file named argv[1], not use the argument. Please remove the quotes around it and try again. There are a few other possible problems, but that seems like the most glaring one. Any particular reason you want to use C for this?

DMCP2143

Yep it was the quotes, I probably just should have slept on it.

Anyways, it won't take my hex values for input, it reads decimals then writes the hex equivilant. I sort of expected some issues here too.

KingMike

I've never heard of atoi before but Googling it, it seems to convert to decimal.

I write my own function to covert the string to hex, though I imagine there's probably already a C++ function for it.
"My watch says 30 chickens" Google, 2018

DMCP2143

Yes I quickly googled hex command line argument and found it, here's the code for any who may be interested. It handles any hex values I give it, 0xFF, FF or ff, and reads 10 as decimal 16.

#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )  // program.exe file.name hexAddress newByte
{
    FILE *f = fopen( argv[1], "r+b" );
    long int hexAddress = ( long int ) strtol( argv[2], NULL, 16 );
    fseek( f, hexAddress, SEEK_SET );
    unsigned char newByte = ( unsigned char ) strtol( argv[3], NULL, 16 );
    fwrite( &newByte, sizeof( newByte ), 1, f );
    fclose( f );
}



Lola814

Quote from: Photonic on March 15, 2019, 07:23:23 PM
Yes I quickly googled hex command line argument and found it, here's the code for any who may be interested. It handles any hex values I give it,  0xFF, FF or ff, and reads 10 as decimal 16.

#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )  // program.exe file.name hexAddress newByte
{
    FILE *f = fopen( argv[1], "r+b" );
    long int hexAddress = ( long int ) strtol( argv[2], NULL, 16 );
    fseek( f, hexAddress, SEEK_SET );
    unsigned char newByte = ( unsigned char ) strtol( argv[3], NULL, 16 );
    fwrite( &newByte, sizeof( newByte ), 1, f );
    fclose( f );
}


Ve never heard of atoi before but Googling it, it seems to convert to decimal.

I write my own function to covert the string to hex, though I imagine there's probably already a C++ function for it.