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 7536241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:31:14+00:00 2026-05-30T06:31:14+00:00

I’d like to make copy of my binary file, but I need to make

  • 0

I’d like to make copy of my binary file, but I need to make it from hex representation of my binary file.

In the first program I create txt file with with hex representation of my binary file:

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

const int BYTE = 1;
int counter = 0;
int read;
long size;
FILE *file1 = NULL; 
FILE *file2 = NULL; 
fpos_t length;

int main() {

        unsigned char hex[3];
        unsigned char buffer[1];
        file1 = fopen("server.pdf", "rb");
        fseek(file1, 0, SEEK_END);
        fgetpos(file1, &length);
        size = length.__pos;
        fseek(file1, 0, SEEK_SET);
        if (file1) {
                file2 = fopen("test.txt", "w");
                while (counter < size) {
                        read = fread(buffer, 1, BYTE, file1);
                        counter += read;
                        i = 0;
                        while(i<read) {
                        sprintf(hex, "%02x", (unsigned int) buffer[i++]);
                        fwrite(hex, 1, BYTE, file2);
                        }
                }
        } else
        printf("ERROR");
        fclose(file1);
        fclose(file2);


}

In the second, I read data from txt file and after that I write it to binary file:

#include <stdio.h>

FILE *file1;
FILE *file2;
int size;
fpos_t length;
int main(){
        file1 = fopen("test.txt", "r");
        fseek(file1, 0, SEEK_END);
        fgetpos(file1, &length);
        size = length.__pos;
        fseek(file1, 0, SEEK_SET);
        char buffer[1];
        char hex[3];
        int counter = 0;
        int read;
        if(file1){
                file2 = fopen("test.pdf", "wb");
                while (counter < size) {
                read = fread(hex, 1, 3, file1);
                counter += read;
                sscanf(hex, "%02x", buffer);
                fwrite(buffer, 1, 1, file2);
                }
        }
        fclose(file1);
        fclose(file2);
}

Unfortunately I can’t open my copy. What is the reason?

  • 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-05-30T06:31:15+00:00Added an answer on May 30, 2026 at 6:31 am

    Have you looked at the files content? You wont be able to sprintf the hex representation to the variable hex since it’s 1 byte in size.

    The variable hex is declared hex[BYTE] where BYTE = 1, but your sprintf format string looks like this: "%02x" ie 2 bytes, then you need room for a terminating zero.

    The same goes for when you write to the file, you only write 1 byte from your hex string.

    Declaring a variable as: var[1] is pointless you can achieve the same thing with var btw.

    Besides this you should also add proper error handling, if you can not successfully open the file. This means checking the file pointer after your call to fopen, then take an appropriate action. perror() will print an error string that corresponds to errno, and in case of a file that does not exist it will print something like: “no such file or directory” or similar.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.