0

I have a text file with some hexadecimal data inside.

I can open it in PHP, and output a full line on the webpage, but i want to parse this result into different lines

<?php

$file_handle = fopen("result.txt", "rb");    
while (!feof($file_handle)) {
    $line_of_text = fgets($file_handle);
    $parts = explode('=', $line_of_text);
    print $parts[0] . $parts[1]. "<BR>";
}

fclose($file_handle);

This reads the txt. file and it displays on the web page the following data

20d73ef83df97d721c14c4340f45fae151b4

but i need to parse this result to something like this displayed in text boxes

Key1 = 20D7 Message ciphered = 73EF83DF97D7 IV = 21C14C4340 Result key = F45FAE151B4

and so on until it finishes parsing last bytes, some lines have bigger length then others.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    If the input is always formed like so you could use `substr()` to initially grab the relevant portions, then de-hex the results with like fashion this SO thread https://stackoverflow.com/questions/14674834/php-convert-string-to-hex-and-hex-to-string – GetSet Jan 20 '20 at 02:59
  • please [edit] your question to provide comments as to what your code does. – hongsy Jan 20 '20 at 03:48
  • You have an error in the displayed data or your expected result is incorrect: 20d**7**3 – jspit Jan 20 '20 at 06:46
  • Nobody will be able to tell you what's wrong without seeing your text file. – miken32 Jan 20 '20 at 20:20

1 Answers1

0

The function sscanf can be used to split a string. The exact number of characters must be known for each part.

Example:

$string = '20d773ef83df97d721c14c4340f45fae151b4';
list($Key1, $Message_ciphered, $IV, $Result_key) = sscanf($string, "%4s%12s%10s%10s");

List was used here to show that sscanf provides an array here. File can be used To read a file line by line into an array.

jspit
  • 7,276
  • 1
  • 9
  • 17