Skip to content

Instantly share code, notes, and snippets.

@Cvar1984
Created October 18, 2021 13:48
Show Gist options
  • Save Cvar1984/2e18a57846a5a80797ad52609f842634 to your computer and use it in GitHub Desktop.
Save Cvar1984/2e18a57846a5a80797ad52609f842634 to your computer and use it in GitHub Desktop.
<?php
/**
* BinaryUtils.php
* Copyright (c) 2021 Cvar1984 <[email protected]>
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
namespace Cvar1984\Utils;
class BinaryUtils
{
public static function binaryToHex(string $field) : string
{
$hexField = bin2hex($field);
$hexField = chunk_split($hexField, 2, '\\x');
$hexField = '\\x' . substr($hexField, 0, -2);
return $hexField;
}
public static function hexToBinary(string $field) : string
{
$binaryField = explode('\\x', $field);
$binaryField = implode('', $binaryField);
$binaryField = hex2bin($binaryField);
return $binaryField;
}
public static function fromFile(string $fileName) : string
{
$binaryField = file_get_contents($fileName);
return $binaryField;
}
public static function toFile(string $fileName, string $file) : bool
{
return file_put_contents($fileName, $file);
}
}
$fileName = $argv[1];
$file = BinaryUtils::fromFile($fileName);
$hexField = BinaryUtils::binaryToHex($file);
$binaryField = BinaryUtils::hexToBinary($hexField);
echo BinaryUtils::toFile($fileName . '.img', $binaryField);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment