I have base 64 encoded string that looks something like this.
cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=
I have tried base64_decode and output is.
råkq‹å©ŒÞÜæ|c›R©6Ó™œªë´Áäw¸lø
I think I may be doing something wrong. I appreciate any help to convert base64 string to binary array.
Thanks
like this
$a = base64_decode("cuVrcYvlqYze3OZ8Y5tSqQY205mcquu0GsHkgXe4bPg=");
$b = array();
foreach(str_split($a) as $c)
$b[] = sprintf("%08b", ord($c));
print_r($b);
You already are getting binary data back from base64_decode (if the encoded data was in fact binary), only this binary data is interpreted as encoding for some text by whatever you're outputting to (browser?). A "0011010110011001" output itself would also only be text, which would be encoded using some (different) binary stream. The computer does not work with 1's and 0's internally, contrary to popular believe. If you want to visualize binary data in the form of 1's and 0's, you'll need to do the binary/text conversion yourself. Usually that's a pretty pointless thing to do, though.
You're probably already doing the right thing. Your mistake is in expecting binary data to be represented as "0100101010".
Related
I'have a $value like that var_dump(bin2hex($value)) output = '015180'
I want the convert this value to decimal. So I know my $value is binary, I write those codes:
var_dump(bindec($value));// 0
var_dump(hexdec(bin2hex($value)));//86400
Result is 86400 but why don't the first one returns me 0. what is the different between two of them. Or Am I missing something ?
I'm only quoting one of comments in bin2hex documentation:
bin2hex function is for converting binary data into a hexadecimal string representation. This function is not for converting strings representing binary digits into hexadecimal.If you want that functionality, you can simply do this:
<?php
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;
?>
This would output "f9". Just remember that there is a very big difference between binary data and a string representation of binary.
Author: tehjosh
I have a problem with encoding php array to json. Part of my data are stings like '555', '3M', part of data are numbers: floats or integers. On frontend I need to receive strings as strings and numbers as numbers as numbers for correct sorting, searching , etc. Php has a JSON_NUMERIC_CHECK, but this will convert my sting '555' value into number. Is there any approach except first converting strings like strings, then numbers with JSON_NUMERIC_CHECK option and then concatenating result?
$a = json_encode(['555', '3M', 123, 1.2, 1.3]);
var_dump($a);//outputs string(24) "["555","3M",123,1.2,1.3]"
Maybe you send data the wrong way. Try to see what you receive after your AJAX (POST method). Or read how correctly send data with AJAX.
http://api.jquery.com/jquery.ajax/
Actually, the problem was with numbers. Because I queried numeric data from db, I received it as strings. So I converted string to numbers, and received correctly encoded data on frontend:
array_walk_recursive($data, function(&$val) {
if (is_numeric($val)) $val = (float) $val;
});
I have to send the JSON:
{"val": 5000.00}
Neither {"val": "5000.00"} nor {"val": 5000} is correct format.
json_encode() converts 5000.00 to 5000
Is it possible to send correct json's format (two zeros) with json_encode from
array("val" => (float) 5000.00) ?
No, it's not possible, because what you think are "correct" and "incorrect" values are actually the same value. It's purely a rendering decision to show/hide trailing zeros.
It's up to you at display time to render the value with the correct number of decimal places. You can't force a floating point number to be stored or transfered with a certain number of decimals.
If you really really need to do that you may consider two options:
Option nr. 1:
You build up the json econded string by your own. If the data you have to encode have a simple structure and that structure is not subject to change in the future the task is easy. You'll have a slower script so another requirement is that the data is not too much.
For a single object like the example you posted...
$json = sprintf('{"val": %.2f}', $floatValue);
Of course for structured data, like an array of objects, arrays of arrays... you'll have to write the necessary loops, place accurately , : [ ] { } ". Hope I gave you the idea...
Option nr. 2:
You build up the data you have to encode just as you would if you didn't have the weird requirement you asked for. But store float values into strings that will also contains some pattern characters.
For example you'll store 5000.00 as "##5000.00##"
Encode the data with json_ecode.
Permorm string replacements to eliminate "## and ##"
$floatValue = 5000.00;
$data = array("val" => sprintf("##%.2f##", $floatValue));
$json = json_encode($data);
$json = str_replace('"##', '', $json);
$json = str_replace('##"', '', $json);
Choose your pattern characters (## in the example) carefully to avoid conflicts with other strings that may contain the same pattern elsewere in you data to be encoded.
just preg_replace('/:"(\d+\.\d+)",/', ':$1,', json_encode($a, JSON_FORCE_OBJECT))
I'm using this code:
$url = "http://www.webtoolkit.info/javascript-base64.html";
print base64_encode($url);
But the result is very long: "aHR0cDovL3d3dy53ZWJ0b29sa2l0LmluZm8vamF2YXNjcmlwdC1iYXNlNjQuaHRtbA=="
There is a way to transform long string to short encryption and to be able to transform?
for example:
new_encrypt("http://www.webtoolkit.info/javascript-base64.html")
Result: "431ASDFafk2"
encoding is not encrypting. If you're depending on this for security then you're in for a very nasty shock in the future.
Base 64 encoding is intended for converting data that's 8 bits wide into a format that can be sent over a communications channel that uses 6 or 7 bits without loss of data. As 6 bits is less than 8 bits the encoded string is obviously going to be longer than the original.
This q/a might have what you're looking for:
An efficient compression algorithm for short text strings
It actually links here:
http://github.com/antirez/smaz/tree/master
I did not test it, just found the links.
First off, base64 is an encoding standard and it is not meant to encrypt data, so don't use that. The reason your data is so much longer is that for every 6 bits in the input string, base64 will output 8 bits.
There is no form of encryption that will directly output a shortened string. The result will be just as long in the best case.
A solution to that problem would be to gzip your string and then encrypt it, but with your URL the added data for the zip format will still end up making your output longer than the input.
There are a many different algorithms for encrypting/decryption. You can take a look at the following documentation: http://www.php.net/manual/en/function.mcrypt-list-algorithms.php (this uses mcrypt with different algorithms).
...BUT, you can't force something to be really small (depends on the size you want). The encrypted string needs to have all the information available to be able to decrypt it. Anyways, a base64-string is not that long (compared with really secure salted hashes for example).
I don't see the problem.
Well... you could try using md5() or uniqid().
The first one generate the md5 hash of your string.
md5("http://www.webtoolkit.info/javascript-base64.html");
http://php.net/manual/en/function.md5.php
The second one generates a 13 unique id and then you can create a relation between your string and that id.
http://php.net/manual/en/function.uniqid.php
P.S. I'm not sure of what you want to achieve but these solutions will probably satisfy you.
You can be creative and just do some 'stuff' to encrypt the url so that it is not easy quess able but encode / decode able..
like reverse strings...
or have a random 3 letters, your string encoded with base64 or just replace letters for numbers or numbers for letters and then 3 more random letters.. once you know the recipe, you can do and undo it.
$keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
$length = 2;
$randkey = "";
$randkey2 = "";
for ($i=0;$i<$length;$i++) $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1);
I have stored binary data in Mysql field like this: 0x31 and 0x31303030303332 . this was converted to binary data from string, I don't know that string and I don't know how string had converted to this binary data.
As far as I know the pack() and unpack() PHP functions can help me, BUT I can't convert back this binary data to string because I don't know what format (second argument of unpack() function) had been used.
How can I find correct format to add in unpack('format', $mybinarystring) function?
Or may be this can be converted back via another function or method?
There is no way to definitely find out which format is used to create the binary data, because its just binary data. You can guess, look at the content and find a hint, or just try&error.