News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Help- command line hex edit tool  (Read 1340 times)

Photonic

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Help- command line hex edit tool
« on: March 14, 2019, 09:42:01 pm »
I want to make a command line hex edit tool.
program.exe file.name hexAddress newByte

This works.
Code: [Select]
#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.

Code: [Select]
#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

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6894
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: Help- command line hex edit tool
« Reply #1 on: March 15, 2019, 01:20:19 am »
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

  • Sr. Member
  • ****
  • Posts: 305
    • View Profile
    • mteam
Re: Help- command line hex edit tool
« Reply #2 on: March 15, 2019, 05:16:32 am »
Another obvious mistake is:
Code: [Select]
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?
« Last Edit: March 15, 2019, 07:05:40 am by mziab »

Photonic

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: Help- command line hex edit tool
« Reply #3 on: March 15, 2019, 03:57:49 pm »
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

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6894
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: Help- command line hex edit tool
« Reply #4 on: March 15, 2019, 06:43:01 pm »
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

Photonic

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: Help- command line hex edit tool
« Reply #5 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>

Code: [Select]
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

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Help- command line hex edit tool
« Reply #6 on: September 04, 2019, 03:20:42 am »
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>

Code: [Select]
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.