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

how to pack() several formats and write the binary string into a db?

Hi,

I tried to pack eight integer values and one string into one binary
string, which I would like to store in a mysql db.
I encountered two problems doing this:

1)
$this->packed = pack('N8A*', $this->value1, $this->value2,
$this->value3, $this->value4, $this->value5, $this->value6,
$this->value7, $this->value8, $this->stringvalue);
$array = unpack('N8A*', $this->packed);
print_r($array);

returns:
Array ( [A*1] => 0 [A*2] => 0 [A*3] => 2 [A*4] => 0 [A*5] => 0 [A*6]
=> 91 [A*7] => 0 [A*8] => 1 )
instead of packing the string also. 'N8 A*' doesn't work at all.

2)
I started storing only the seven integers in the binary string and
wanted to write them into the database.
I used a column "packed varchar(50) binary not null default '';".
To store the data I used
$packed = addslashes($classname->packed);
mysql_query("UPDATE $table SET packed='$packed' WHERE ...");
as well as without addslashes().
I also changed the column type to "packed tinyblob not null;" what I
didn't liked much as I would like to reduce the amount of space as
much as possible.
Whatever I did, I got a string back that looked simular when I print
it, but it didn't unpack() to the former values.
Now I have two questions:
- How can I pack several data types into one binary string (8
integers and one string) using pack()? How should the format string
look like?
- How can I store such a binary string in a database?

Thanks in advance,
Johannes
Jul 17 '05 #1
1 2282

"Johannes" <sp*********@lycos.de> wrote in message
news:f1*************************@posting.google.co m...
Hi,

I tried to pack eight integer values and one string into one binary
string, which I would like to store in a mysql db.
I encountered two problems doing this:

1)
$this->packed = pack('N8A*', $this->value1, $this->value2,
$this->value3, $this->value4, $this->value5, $this->value6,
$this->value7, $this->value8, $this->stringvalue);
$array = unpack('N8A*', $this->packed);
print_r($array);

returns:
Array ( [A*1] => 0 [A*2] => 0 [A*3] => 2 [A*4] => 0 [A*5] => 0 [A*6]
=> 91 [A*7] => 0 [A*8] => 1 )
instead of packing the string also. 'N8 A*' doesn't work at all.


Read the manual carefully. You have to assign names to the variables you're
unpacking.
Jul 17 '05 #2

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

Similar topics

4
by: Stacy Mader | last post by:
Greetings all, I have a VMS binary file with weather data. The record on each line is 1xint(8) 12xint(2) Using the perl unpack function, I can decode the binary file like this: <snip>
3
by: Wiktor Zychla | last post by:
I would like to be able to convert enums to their string representation but I would like to be able to apply a custom formatting, so that I could write for example: enum E { a, b, c }; string...
2
by: Mark Jerde | last post by:
(If these are the wrong groups please suggest the right one(s). Thanks.) I need to come up with a way to test potentially thousands of data (files / records / streams) to determine if they match...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
20
by: tomerfiliba | last post by:
hey i've been seeing lots of config-file-readers for python. be it ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the like. seems like a trend to me. i came to this conclusion...
4
by: gogaz | last post by:
Hi, PHP has a function called pack() (http://us2.php.net/pack). I need to translate some code that uses this function to C#. What's the C# equivalent of pack()? Basically in php $i = 1038;...
3
by: craigkenisston | last post by:
I need to send a big amount of data to one server in the query string. It is like 3kb of text information, in ascii code with linefeeds and carriage returns. I noticed that using winzip I can get...
5
by: andmarti | last post by:
If I build a strict with: import struct print struck.pack ('i', 1) it returns a '\n'. What's wrong with it??? :( --
10
by: giddy | last post by:
hi , I' know what the StructLayoutAttribute does. I've seen it being used. But what does the Pack field do!? This is what MSDN says: Controls the alignment of data fields of a class or...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.