473,396 Members | 1,748 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,396 software developers and data experts.

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 2583
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\x72\x61\x6b\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x75\x05\x1a\x00";
$in2 = "\x41\x6d\x74\x72\x61\x6b\x20\x4f\x6e\x6c\x69\x6e\ x65\x00\x00\x00" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x8f\x05\x2b\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\x72\x61\x6b\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x75\x05\x1a\x00";
$in2 = "\x41\x6d\x74\x72\x61\x6b\x20\x4f\x6e\x6c\x69\x6e\ x65\x00\x00\x00" .
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x8f\x05\x2b\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($string) {
$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($string) {
$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
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:...
8
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...
6
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...
7
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...
8
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
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...
6
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
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...
6
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.