Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:49:37+00:00 2026-06-12T12:49:37+00:00

I wrote some code to apply a patch to a binary file, the patches

  • 0

I wrote some code to apply a patch to a binary file, the patches are delimited ascii representations of hexadecimal values, like so: 00FF00:A155F3;9210BC.

My problem is that I’m miscalculating the value of a hexadecimal by exactly 256, but only between the delimiters ‘:’ and ‘;’ since I use the same hex2char() function regardless of position within the code, I can’t figure out why it’s failing in this one specific area. I figured instead of wasting too much time on it, I’ll sign up for stackoverflow and get some help.

I don’t want to use any external libraries, I don’t want to include anything other than stdio.h and stdlib.h (I’ll remove sys/stat.h, and fread() and realloc() until EOF, but that’s another problem and i’m not concerned about it)

here is the code, my main loop is way near the end and I clearly mark where I get the bad output:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>


//I'll populate this later
struct _pos {
    unsigned pos;
} pos;


//raise x to the power of y
unsigned power( unsigned x, unsigned y )
{
    if ( y == 0 )
    {
        return 1;
    }
    else
    {
        return x * power( x, y - 1 );
    }
}

//convert ascii hex values to integers
unsigned h2i(const char *input, int len) {
    unsigned retval = 0;
    char cur = 0;
    int loops = 0;
    while (0 < len) {
        cur = input[--len];
        //check if we are out of the range hex would be in
        //we assume capital letters A - F
        if ( cur < 0x30 || cur > 0x46 ) {
            printf("Hex out of range: %X\n", cur);
            exit(EXIT_FAILURE);
        }

        //numerical
        if ( cur < 0x3a ) {
            cur -= 0x30;
        }

        //alphabetical
        else {
            cur -= 0x37;
        }

        retval += (cur * power(16, loops));
        ++loops;
        }
    return retval;
}

//take 2 chars from the input and get a hex value from it
char hex2char(const char *input) {
    char buff[2];
    buff[0] = input[pos.pos++];
    buff[1] = input[pos.pos++];
    printf("\nBUFF: %c%c : POS: %i\n", buff[0], buff[1], pos.pos);
    return h2i(buff, 2);
}


//turn a delimiter terminated string into a number
unsigned hex2int(const char *input, char delimiter) {
    int offset = 0;
    char buff[13]; //max int length if represented by string

    //copy string into the buffer
    while (input[offset] != delimiter) {
        if (offset == 12) {
            printf("parse error: offset\n");
            exit(EXIT_FAILURE);
        }
        buff[offset] = input[offset];
        ++offset;
    }
   //printf("BUFF: %s : OFFSET: %i\n", buff, offset);
    pos.pos += offset;
    return h2i(buff, offset);

}

char *fast_cache( char *Filename, unsigned long *size ) {
    FILE *FilePointer;
    FilePointer = fopen( Filename, "r" );
    if ( FilePointer != NULL ) {
        char *ptr;
        struct stat FileStat;
        fstat( fileno( FilePointer ), &FileStat );
        *size = FileStat.st_size;
        ptr = ( char * ) malloc( *size );
        fread( ptr, 1, *size, FilePointer );
        fclose( FilePointer );
        return ptr;
        }
    else {
        return NULL;
        }
    }


int main(int argc, char **argv)
{
    // print greeting
    printf("Hello World!\n");

    unsigned long size;
    char *file = fast_cache(argv[1], &size);
    if (file == NULL) {
        printf("You must specify an input!\n");
        exit(EXIT_FAILURE);
    }

    //okay, patchfile found, opened, and cached, time to convert
    pos.pos = 0; //file position

    //this will be the output, unused right now
    FILE *out = fopen("output", "w");
    //a line in the file is formatted like this
    //0000FF:A9DF23;A7FF11
    unsigned fileoffset = 0;
    unsigned trash = 0;


    //loop through all lines in the patchfile
    while (pos.pos < size) {
        fileoffset = hex2int(file, 0x3a);
        ++pos.pos;
        printf("offset: %u CUR: %c\n", fileoffset, file[pos.pos]);
        while(file[pos.pos] != 0x3b ) {
            //check current values with file values
            //not implimented yet, i just print it out

///////////////////////////////////////////////////////////////////
///////////This is the issue right here///////////////////////////
//printf prints a near MAX_INT number hex value > 0xFFFFFF00
//and it is off by exactly 256 every time, but that would mean overflowing
            printf("%X ", hex2char(file));

        }
//////////////////////////////////////////////////////////////////
//but right here until EOF, everything is fine and works normally
//and it's the same freaking function

        printf("\nfound delimiter: %c @ %u\n", file[pos.pos], pos.pos);
        ++pos.pos;

        //second half of the line
        while(file[pos.pos] != '\n' && pos.pos < size) {
            printf("%X ", hex2char(file));
        }
        //eat the new line if there is one
        ++pos.pos;
        printf("\nLine complete\n");
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:49:38+00:00Added an answer on June 12, 2026 at 12:49 pm

    When using printf in C, your chars will be promoted to ints. As they are unsigned chars, you have to mask the remaining bits.

    Replace:

    printf("%X ", hex2char(file));
    

    with:

    printf("%X ", hex2char(file) & 0xff);
    

    You can find a complete description of this problem here:
    Printing hexadecimal characters in C
    and here http://en.wikipedia.org/wiki/Sign_extension

    If you have to implement this in assembly, I recommend replacing your pow function by a simple bit shifting operation. X << 4 == X * 16. Also pay attention that on microcontrollers or other embedded systems, you can find a 16 bit system. In this case, the size of your int can be different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code to read a file in my Java Servlet class. (I'm
I wrote some code in VHDL that is expected to look at a rotory
I wrote some code that activity starts a service to get some text from
I wrote some code which monitors the output of Stopwatch using a tight loop.
I wrote some code (Java and LDAP) to create a user in the Active
I wrote some code for the ising model in python (2d). Everything looks seems
I wrote some code today and It was changed by another developer who said
I have wrote some code to find out of 3 variables witch is the
I just wrote some code to test the behavior of std::equal, and came away
Some time ago, I wrote some code to decide which method of updating mutable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.