orbit/src/Orbit/Util.php

42 lines
991 B
PHP

<?php declare(strict_types=1);
namespace Orbit;
class Util
{
/**
* View hex chars of string
*
* Outputs a listing of hexidecimal values in 16 byte rows
*
* @param string $text Input text
* @return string
*/
public static function hexView(string $text): string
{
$width = 16;
$outStr = '';
// Specifically not using mb_strlen to get every byte
$charCount = strlen($text);
for ($i = 0; $i < $charCount; $i += $width) {
$printStr = '';
for ($j = 0; $j < $width; $j++) {
$char = (string) substr($text, $i+$j, 1);
$outStr .= sprintf("%02X", ord($char)) . " ";
if (ord($char) >= 32 && ord($char) < 127) {
$printStr .= $char;
} else {
$printStr .= ".";
}
}
$outStr .= " | " . $printStr . "\n";
}
return $outStr;
}
}