473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Picking out integers from a binary character string

I'm working on a program that manipulates bmp files. I know the offset
location of each piece of relevent data within the bmp file. For
example, I know the 18th through 21st byte is an integer value
representing the width of the bmp image. So far, I have been able to
use fstream's seek, write, and read to pull out chucks of bytes and
convert them to usable integers straight out of the binary file. It
works well, but over the course of my program there are thousands of
writes and reads, which I imagine is not the most efficient way of
processing the data.

Instead, I want to read in the entire bmp binary data into one
character string, say "buffer". I want to then pick out 4-byte chunks
of buffer and convert them to usable integers, do all of the necessary
processing, then write the whole buffer back to a new bmp file. This
would require only one write and one read. My experience with binary
character manipulation is, however, very limited and I have not yet
been able to pick out the individual bytes from buffer that I want.
Here's an example of how I'm trying it:

<******>
char buffer[25000000]; //buffer can handle a bmp up to 25MB
int fileSize;
int width;

ifstream bmp ("image.bmp", ios::binary); //open bmp for binary input
bmp.seekg(0, ios::end); //put cursor at end of file
fileSize = bmp.tellg(); //offset of last byte = size of file
bmp.seekg (0, ios::beg); //put cursor at beginning of file
bmp.read (buffer, fileSize); //read entire bmp into char string buffer

width = (int)buffer[18];
cout << width << endl;
<******>

Instead of setting the 18th, 19th, 20th, and 21st byte into "width", it
just sets the 18th byte by itself. I was hoping c++ would be smart
enough to realize that since "int width" was 4 bytes by definition, it
would know to fill in the other 3 appropriately. So my question is how
would I get all 4 bytes into "width" as an integer value.

Jul 23 '05 #1
1 3445
ru******@gmail.com wrote:
I'm working on a program that manipulates bmp files. I know the offset
location of each piece of relevent data within the bmp file. For
example, I know the 18th through 21st byte is an integer value
representing the width of the bmp image. So far, I have been able to
use fstream's seek, write, and read to pull out chucks of bytes and
convert them to usable integers straight out of the binary file. It
works well, but over the course of my program there are thousands of
writes and reads, which I imagine is not the most efficient way of
processing the data.

Instead, I want to read in the entire bmp binary data into one
character string, say "buffer". I want to then pick out 4-byte chunks
of buffer and convert them to usable integers, do all of the necessary
processing, then write the whole buffer back to a new bmp file. This
would require only one write and one read. My experience with binary
character manipulation is, however, very limited and I have not yet
been able to pick out the individual bytes from buffer that I want.
Here's an example of how I'm trying it:

<******>
char buffer[25000000]; //buffer can handle a bmp up to 25MB
int fileSize;
int width;

ifstream bmp ("image.bmp", ios::binary); //open bmp for binary input
bmp.seekg(0, ios::end); //put cursor at end of file
fileSize = bmp.tellg(); //offset of last byte = size of file
bmp.seekg (0, ios::beg); //put cursor at beginning of file
bmp.read (buffer, fileSize); //read entire bmp into char string buffer

width = (int)buffer[18];
cout << width << endl;
<******>

Instead of setting the 18th, 19th, 20th, and 21st byte into "width", it
just sets the 18th byte by itself. I was hoping c++ would be smart
enough to realize that since "int width" was 4 bytes by definition, it
would know to fill in the other 3 appropriately. So my question is how
would I get all 4 bytes into "width" as an integer value.


Hmm, well an int MAY be 4 bytes, and it may not. It all depends
on the machine's architecture (16/32/64 bit). The 'int' at
'buffer[18]' may, or may not, be in the correct format for your
hardware (big-endian, little-endian). You may, or may not, be
able to read/write an 'int' at a particular memory address - it
all depends on whether or not your hardware requires a particular
address alignment for 'int'. Now, having considered all of this,
you could do:

int width;
width = *((int *)(buffer + 18));

- 'buffer' is a char pointer
- So, 'buffer + 18' is a char pointer to the
19th char (remember, the subscripts start at zero).
- You want an 'int', so cast 'buffer + 18' to an
int pointer.
- Assign what-the-pointer-points-to (i.e. *thePointer)
to 'width'.

Using C++ static-cast would be better than using the
C-style '(int *)' cast.

Regards,
Larry
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
2
by: Rakesh Sinha | last post by:
Hi, I am writing this application in C++. It reads data from binary files. My current requirement is that: Given a positive number N, I have to read in N bytes from the input stream (which is...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
10
by: jt | last post by:
I'm needing to take a binary string start at a certain position and return a pointer from that postion to the end of the binary stirng. something like this: char bstr; char *pos; ...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
9
by: ruffiano | last post by:
Hi, can someone tell me if a C++ string (std::string) represents a binary or an ASCII string? Thanks in advance.
3
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I don´t understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write,...
1
by: farahomer | last post by:
Hi !! I have a question. I am developing a code in C#.NET. So here I have a long string in this format: 000000000010 000000100100 (for example). Its 24 digit long, split into 12 digits by a space in...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.