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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:16:57+00:00 2026-05-27T11:16:57+00:00

Hi I am reading in a binary file formatted in hex. It is an

  • 0

Hi I am reading in a binary file formatted in hex. It is an image file below is a short example of the first few lines of code using hd … |more command on linux. The image is a binary graphic so the only pixel colours are either black or white. It is a 1024 by 1024 image however the size comes out to be 2097152 bytes

00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |…………….|

000dfbf0 00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff 00 |…………….|

000dfc00 ff 00 ff 00 ff 00 00 00 00 00 00 00 00 00 00 00 |…………….|

000dfc10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |…………….|

This is the code I am using to read it in found in another thread on SO

ifstream file (argv[1], ios::in | ios::binary | ios::ate);
ifstream::pos_type fileSize;
char* fileContents;
if(file.is_open())
{
    fileSize = file.tellg();
    fileContents = new char[fileSize];
    file.seekg(0, ios::beg);
    if(!file.read(fileContents, fileSize))
    {
        cout << "fail to read" << endl;
    }
 file.close();
 cout << fileSize << endl;

The code works however when I run this for loop

for (i=0; i<2097152; i++)
 printf("%hd",fileContents[i]);

The only thing printed out are zeros and no 1s. Why is this are my parameters in printf not correctly specifying the pixel size. I know for a fact that there are 1’s in the image representing the white areas. Also how do i figure out how many bytes represent a pixel in this image.

  • 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-27T11:16:58+00:00Added an answer on May 27, 2026 at 11:16 am

    Your printf() is wrong. %hd means short, while fileContents[i] is a char; on all modern systems I’m familiar with, this is a size mismatch. Use an array of short instead, since you have twice as many bytes as pixels.

    Also, stop using printf() and use std::cout, avoiding all type mismatch problems.

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

Sidebar

Related Questions

While reading a binary file using DJGPP on DOS this code hangs. This happens
I'm tasked with reading a poorly formatted binary file and taking in the variables.
Using Windows So I'm reading from a binary file a list of unsigned int
When reading two dates from a binary file I'm seeing the error below: The
My code for reading binary file is: dataFile.open(fileName.c_str()); ifstream binData(Trafficlog_Data.txt, ios::in | ios::binary); //binary
I'm using Qt for reading binary file. I read file with QFile object, but
I'm reading a binary file using perl. In the file's headers, there's 4 bytes
I am having following issue with reading binary file in C. I have read
I am reading a binary file into a parsing program. I will need to
I am reading a binary log file produced by a piece of equipment. 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.