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

Handling Binary Strings

I am trying to handle binary strings in php. I get a binary output
initialization vector from mcrypt as such:

from mcrypt:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

This output may have padded nulls at the end which are significant.

I would like to convert all characters including all padding nulls at
end and convert it in a string to be saved into a database in such a way
that I can reconstruct it back to binary again later.

base64_encode and decode is not doing the job. They don't seem to
preserve the original binary value after base64_decode().

Can someone suggest some other way to preserve binary data into ASCII
and back in php?
Nov 5 '06 #1
2 2714
CharlesL wrote:
I am trying to handle binary strings in php. I get a binary output
initialization vector from mcrypt as such:

from mcrypt:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

This output may have padded nulls at the end which are significant.

I would like to convert all characters including all padding nulls at
end and convert it in a string to be saved into a database in such a way
that I can reconstruct it back to binary again later.

base64_encode and decode is not doing the job. They don't seem to
preserve the original binary value after base64_decode().
What is a padded null?

Whit plain nulls, base64_encode() and base64_decode() work for me.

<?php
$data = "abcde\0"; // trailing null
$in = base64_encode($data);
$out = base64_decode($in);

echo strlen($data), " : ", strlen($out), "\n";
for ($i=0;$i<strlen($data);++$i) {
if ($data{$i} == $out{$i}) {
echo ord($data{$i}), " ok\n";
} else {
echo ord($data{$i}), ";", ord($out{$i}), " NOT ok\n";
}
}
?>

Nov 5 '06 #2
On Sun, 05 Nov 2006 13:45:04 -0500, CharlesL <cl@sd.nlwrote:
>I am trying to handle binary strings in php. I get a binary output
initialization vector from mcrypt as such:

from mcrypt:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

This output may have padded nulls at the end which are significant.

I would like to convert all characters including all padding nulls at
end and convert it in a string to be saved into a database in such a way
that I can reconstruct it back to binary again later.

base64_encode and decode is not doing the job. They don't seem to
preserve the original binary value after base64_decode().
What version are you using? I'm not seeing trimming of trailing nul bytes on
PHP 5.2.0, at least:

andyh@excession /cygdrive/c/public_html
$ cat test.php
<?php
$data = str_repeat(chr(0), 16);

var_dump(unpack('H*', $data));

print base64_encode($data) . "\n";

var_dump(unpack('H*', base64_decode(base64_encode($data))));
?>

andyh@excession /cygdrive/c/public_html
$ php test.php
array(1) {
[1]=>
string(32) "00000000000000000000000000000000"
}
AAAAAAAAAAAAAAAAAAAAAA==
array(1) {
[1]=>
string(32) "00000000000000000000000000000000"
}

(And incidentally pack/unpack may give you another option)

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Nov 5 '06 #3

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

Similar topics

11
by: Laurent Therond | last post by:
Maybe you have a minute to clarify the following matter... Consider: --- from cStringIO import StringIO def bencode_rec(x, b): t = type(x)
3
by: Ryan N. | last post by:
Hello, I saw a brief blurb on this somewhere and am unable to recall where... In the context of Security, what are some best practices for handling -storing, locating, retrieving- database OLEDB...
2
by: Pete | last post by:
Sorry for the ambiguity of my last post, What I am try to do is enter a 10 bit binary string eg: 1110001010 and then permute them into an array using an array containing 3,5,2,7,4,10,1,9,8,6 as...
8
by: Bruce Lee | last post by:
Hi I have the following 2 questions: 1.How to write binary data to a text file in C? I have a number like 10001010110001 say, I need to put it in the text file so that I can see the corresponding...
3
by: ruben.de.visscher | last post by:
I am trying to write a program that encrypts 8-bit plaintext using a 10-bit key. To generate subkeys, and for other things, i will need to be able to perform permutations on binary strings for...
2
by: Feng | last post by:
Hi, I need a VB.Net function that reads in a stream of binary data coming in from a legacy data source. The data are actually hex numbers in binary format but the problem is that I don't know...
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.
1
by: Hatty | last post by:
I am not very familiar with C++, but i need to write a patch for reading a binary file using hex editor and break it into bloks of FFF using start and end addtess and store it into a binary files.
13
by: kboyd | last post by:
Dear all, I'm new to the forum, I'm really sorry if this has appeared elsewhere. I'm not really a programmer and am having major difficulties handling a binary stream which I am trying to read and...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
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
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
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.