473,770 Members | 4,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading data from a simple binary file

I've read some discussions, searched Google, tried pack, unpack,
bin2hex, explode, implode, and I just can't figure out how to convert
this data. I am trying to read a file with the following format (two
examples).

41 6D 74 72 61 6B 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 75 05 1A 00

41 6D 74 72 61 6B 20 4F 6E 6C 69 6E 65 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 8F 05 2B 00

The first 28 bytes are a 00 terminated character string (in this case
"Amtrak" and "Amtrak online")
Next are the location (offset) of an entry in another file (2 bytes),
followed by that entry's size in the file (the next 2 bytes). The above
examples are key entries in an index file pointing to variable sized
entries in a data file.

I am using fread to put each field into a string variable (28 bytes, 2
bytes, and 2 bytes). The binary values are "Little Endian" (right?). In
other words 75 05 is really x0575 and 1A 00 is really x001A. I am having
the most trouble with these (I've already used bin2hex, but I don't know
how to convert the little endian to decimal.)

1. I want to strip the 00's from the string.

2. I want to convert the location (offset) into an integer variable.

3. I want to convert the size into an integer variable.

I am going to use location and size to fseek and fread the entries in
the data file into another string.

My actual goal is to transfer this data (the string and it's associated,
variable sized text field) into a MySQL database.

My guess is (was) that this is easy, but I just can't figure it out.

Any help would be very much appreciated.

TIA.

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*************** **************
Jul 17 '05 #1
4 2603
Chuck Anderson wrote:
41 6D 74 72 61 6B 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 75 05 1A 00

41 6D 74 72 61 6B 20 4F 6E 6C 69 6E 65 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 8F 05 2B 00

The first 28 bytes are a 00 terminated character string (in this case
"Amtrak" and "Amtrak online")
Next are the location (offset) of an entry in another file (2 bytes),
followed by that entry's size in the file (the next 2 bytes). The above
examples are key entries in an index file pointing to variable sized
entries in a data file. I am going to use location and size to fseek and fread the entries in
the data file into another string.


Something like this?

<?php
$in1 = "\x41\x6d\x74\x 72\x61\x6b\x00\ x00\x00\x00\x00 \x00\x00\x00\x0 0\x00" .
"\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00\x00 \x00\x75\x05\x1 a\x00";
$in2 = "\x41\x6d\x74\x 72\x61\x6b\x20\ x4f\x6e\x6c\x69 \x6e\x65\x00\x0 0\x00" .
"\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00\x00 \x00\x8f\x05\x2 b\x00";

$arr = array();
$arr[] = unpack('a28name/vlocation/vsize', $in1);
$arr[] = unpack('a28name/vlocation/vsize', $in2);

var_dump($arr);
echo "\n";
?>
Output is (reformatted):
array(2) {
[0] => array(3) {
["name"] => string(6) "Amtrak"
["location"] => int(1397)
["size"] => int(26)
}
[1] => array(3) {
["name"] => string(13) "Amtrak Online"
["location"] => int(1423)
["size"] => int(43)
}
}
HTH
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Pedro Graca wrote:
Chuck Anderson wrote:
41 6D 74 72 61 6B 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 75 05 1A 00

41 6D 74 72 61 6B 20 4F 6E 6C 69 6E 65 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 8F 05 2B 00

The first 28 bytes are a 00 terminated character string (in this case
"Amtrak" and "Amtrak online")
Next are the location (offset) of an entry in another file (2 bytes),
followed by that entry's size in the file (the next 2 bytes). The above
examples are key entries in an index file pointing to variable sized
entries in a data file.

I am going to use location and size to fseek and fread the entries in
the data file into another string.


Something like this?

<?php
$in1 = "\x41\x6d\x74\x 72\x61\x6b\x00\ x00\x00\x00\x00 \x00\x00\x00\x0 0\x00" .
"\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00\x00 \x00\x75\x05\x1 a\x00";
$in2 = "\x41\x6d\x74\x 72\x61\x6b\x20\ x4f\x6e\x6c\x69 \x6e\x65\x00\x0 0\x00" .
"\x00\x00\x00\x 00\x00\x00\x00\ x00\x00\x00\x00 \x00\x8f\x05\x2 b\x00";

$arr = array();
$arr[] = unpack('a28name/vlocation/vsize', $in1);
$arr[] = unpack('a28name/vlocation/vsize', $in2);

var_dump($arr) ;
echo "\n";
?>
Output is (reformatted):
array(2) {
[0] => array(3) {
["name"] => string(6) "Amtrak"
["location"] => int(1397)
["size"] => int(26)
}
[1] => array(3) {
["name"] => string(13) "Amtrak Online"
["location"] => int(1423)
["size"] => int(43)
}
}
HTH

THVM - That helps very much.

Thank you. I could not understand how to use unpack, but your example
explains it very well.

I only need to add the MySQL functions to load the data into a new
database (which I understand well)

You've saved me hours more of fumbling around ignorantly and the pain of
banging my head on the table while doing it. Thank you very, very much.
(And my head thanks you, too ҿج)

This should be a new thread, but I have one more question I'm guessing
is simple.
One of the key names has chars, then a NULL (x00), and then more chars
(an error got in there somehow.)

It looks like this
52 75 74 68 00 61 6E 00 00 00 00 00 00 00 00 00 - R u t h . a n
00 00 00 00 00 00 00 00 00 00 00 00 C7 53 88 00

So I get Ruth?an ("?" is actually a square - the "no char" symbol).
The "an" does not belong there.

In and of itself, I don't really care. Only 2 entries (of 220 total)
have this problem, but it makes me wonder; how do I make a string
terminate at the first NULL character? I tried sprintf("%s", $string),
but that doesn't do it.

How do you remove all bytes (chars) after the first NULL character?

If you like I could repost this as a new thread, but you seem very
knowledgeable.

And thanks again (very much) for the above!

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*************** **************
Jul 17 '05 #3
Chuck Anderson wrote:
It looks like this
52 75 74 68 00 61 6E 00 00 00 00 00 00 00 00 00 - R u t h . a n
00 00 00 00 00 00 00 00 00 00 00 00 C7 53 88 00

So I get Ruth?an ("?" is actually a square - the "no char" symbol).
The "an" does not belong there. How do you remove all bytes (chars) after the first NULL character?


I don't think PHP has a single function to do that. But it's easy to
make your own :)

<?php
function C_string($strin g) {
$zero = strpos($string, "\x00");
if ($zero === false) return $string;
else return substr($string, 0, $zero);
}

### example usage
$name = "Ruth\x00an\x00 \x00";
$name = C_string($name) ;
?>

--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
Pedro Graca wrote:
Chuck Anderson wrote:
It looks like this
52 75 74 68 00 61 6E 00 00 00 00 00 00 00 00 00 - R u t h . a n
00 00 00 00 00 00 00 00 00 00 00 00 C7 53 88 00

So I get Ruth?an ("?" is actually a square - the "no char" symbol).
The "an" does not belong there.

How do you remove all bytes (chars) after the first NULL character?


I don't think PHP has a single function to do that. But it's easy to
make your own :)

<?php
function C_string($strin g) {
$zero = strpos($string, "\x00");
if ($zero === false) return $string;
else return substr($string, 0, $zero);
}

### example usage
$name = "Ruth\x00an\x00 \x00";
$name = C_string($name) ;
?>

Thanks once again. I always wonder if I should write little loops like
that, but I hate to resort to them only to find out that there is
already a function.

Thanks for helping to clear out the cobwebs of a C++ programmer who
hasn't written any programs in over 12 years. (All I've done is use Php,
now, for almost 2 years.)

BTW, I've got my database migrated and have almost completed the user
interface to it. Thanks a ton. There was a lot of important data in there.

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*************** **************
Jul 17 '05 #5

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

Similar topics

20
3075
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
8
9532
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that contains the following three floating-point numbers: 1.0 2.0 3.0
6
3795
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
7
6063
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
8
2717
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
4
29271
by: flgeyer | last post by:
Hello all, I'm trying to read in a binary file in C. I have the specification of the file format and everything. I can parse the file with a software called "010 Editor". This way I can check if the values I read in in my C program are correct. The problem I have is parsing 32 bit integer values in C. This is the pretty simple code: int temp = 0; fread(&temp,sizeof(int),1,fp);
6
5273
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
11921
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in like this: S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE S is the start of the line, ~ indicates a good read, B is a blank reading, and the numbers 1-6 correspond to a location on the device for a line. So there are six positions on the line...
6
3531
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
4
1506
by: Mastastealth | last post by:
I'm trying to create a program to read a certain binary format. I have the format's spec which goes something like: First 6 bytes: String Next 4 bytes: 3 digit number and a blank byte --- Next byte: Height (Number up to 255) Next byte: Width (Number up to 255) Next byte: Number 0 - 5 Every 2 bytes after that: Supposedly a number 0000 - 0899?
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.