How to write "pseudo" binary into file using php? - php

I have a string containing something like "01001010" and I want to write it into a file using binary. In other words, what's inside that file is not the chars 0/1, but in binary format.
How can I make that?

So you mean you want to convert a string of 0s and 1s (eg $bitString = '01010101...';) into binary data (0x55...), and then write that to a file, you need to do this in two steps.
First, convert your string of zeros and ones into binary - see Converting string of 1s and 0s into binary value, then compress afterwards ,PHP
Note that strings in PHP can store binary data.
Then just write the output to a file, eg using file_put_contents().

Related

Is there way to know data is decimal or binary or hexadecimal?

I need to convert data of which I don't know whether it is
decimal
binary
hexadecimal
to decimal format.
So far, I have tried to use
bindec() to convert binary data to decimal
hexdec() to convert a hexadecimal to decimal
However, I need to determine beforehand whether a number is decimal or hexadecimal.
Is there a way to do that?
The only disctinction between arbitrary bases you can encounter are literals. In PHP, every number are stored as integers or floats.
$binary = 0b1100; // stored as 12
$oct = 014; // stored as 12
$hex = 0xC; // stored as 12
However, if your data is a string, you can use regex to determine it's type, than use hexdec(), octdec(), bindec() functions to convert them to decimal.
OR, you can use the intval() function, which automatically detects the type by the formatting of the string:
intval("0b000"); // bin->dec conversion
intval("014"); // oct->dec conversion
intval("0xC"); // hex->dec conversion
intval("12"); // str->dec cast!!
intval("asd"); // str->dec cast!!
Watch out with unformatted strings, because intval() will turn everything into a number.

Byte arithmetic and manipulation of PHP string

If I've read a binary file into a variable using $data = fread('myfile','rb') how can I work through $data a byte at a time. I also want to perform operations on each byte, such as multiplying it by a number and calculating the modulo with respect to another number.
I can reference the variable as an array using $data[$i], but am I getting bytes with this or possibly multi-byte characters? Also when I do this, I can't then perform calculations on the results, such as $data[$i]*4, which is always zero.
I need work through very large files so the solution needs to be quick.
Thanks
I can reference the variable as an array using $data[$i], but am I getting bytes with this or possibly multi-byte characters?
You'll get bytes. PHP strings are single-byte.
Also when I do this, I can't then perform calculations on the results, such as $data[$i]*4, which is always zero.
Convert a character to a number with ord, make your calculations and, if needed, convert back with chr. Alternatively, convert the whole buffer with unpack('c*', $buf), which gives you a numeric array.

How to write NON ASCII data in one file with php

I must write a binary file with php, but I think that I don't use the correct method.
I use this function:
$ptr = fopen("file.txt", 'wb');
fwrite($ptr, $Str);
fclose($ptr);
I write this string (chaotic representation of 0 and 1):
$Str="00000001001110000000000100000100000000000000001000000010000000010000001100000101000000000000010000000001000000000000010100000";
I thought that opening the file with OpenOffice I would not have seen the text of zeros and ones, but instead I was sure I saw a chaotic sequence of characters.
Why I see the zeros and ones in open office? How can I do to write the raw data with php?
If you write text in a file, even when open in binary mode, you will get text in the file. Your 0 is not stored as a zero bit, but as the ASCII representation of the 0 caracter.
Use the binary format for numbers in PHP, for instance:
$var = OxFF; // equals to 1111 1111 in binary.
More in the manual
You can write any character with the chr() function.
Alternatively, you can do something like "\x0A" (that's a newline character).

What is Unpack's "binary string"

I'm trying to the unpack function. The PHP documentation says
Unpacks from a binary string into an
array according to the given format.
Does the string passed have to be a binary string? and what exactly is a binary string?
A binary string just means data that is in it's base binary format. The data it's self is being displayed as a string to you if you where to echo it, but it would be meaningless without applying it's correct structure to it. So for example a number would not look like a number, because it's in binary. While the data is there, it has to be parsed as a number for it to be readable as a number otherwise it could look like 'abcd'.
if you read down slightly further you will see examples of what binary strings look like
$binarydata = "\x32\x42\x00\xa0";
Also the description of the method clearly says Unpacks from a binary string, so yes it requires a binary string.
more information on binary

How to split a string in PHP

I have a long string resulted from encoding a binary file -an image file- (in base 64). Is there a particulary method (or rule) I should follow when spliting it?
Edit:
I want to write the string to a xml file, but in order to do this I must split it in smaller chunks.
Edit2:
I would like to split it by length (I think that is more appropriate in this case).
Thank you.
You might want to take a look at chunk_split(), since you do base64 encoding. (Assuming you do encoding, since you can't embed decoded base64 data in XML files)
Use explode() or preg_split() as appropriate. All data in PHP is technically binary, even character data. If your data is multibyte character data (eg UTF-8) you'll need extra steps to correctly handle that.
Also binary strings may need processing with unpack().
chunk_split (string $body [, int $chunklen [, string $end ]] )

Categories