Jump to content

How To Convert Text To Hex And Return The Output Reversed.?


Brandon_R

Recommended Posts

Hello i am VERY new to PHP and would like to know how to convert ascii to hex and return the output in reversed order in lines of 4 while generating whats left with zeros. Lets say a person enters the word Hello World in a form and press submit. I would like for the php script to return the output hex values in reversed order (starting from left going right) skipping a line after every 4 while generating whats left (the counter) with zeros so it would be like this.

 

6c6c6548=lleh

6f57206f=oW 0

00646c72=dlr

0000000

 

Please Help Me Accomplish this. Thanks you.

Link to comment
Share on other sites

Thank you sir I think i know why your title says PHP Guru :)

 

But i have just a tiny problem. After the first 4 hexes it doesn't skip a line. It Just keeps gooing line this

 

6e616854 6f59206b 75000000 00000000

 

I think its because instead of using $string = "Hello World";, i changed it to $string = $_POST['ascii_inputs']; and wrote a form to make it user submittable.

 

Is there any way i can get it to skip a line after the first 4 Hexes. Also it would be very nice if you could show me how to put text next to the hexes like.

 

The First Line= 6c6c6548

The Second Line = 6f57206f

The Third Line Equals= 00646c72

The Forth Line Equals = 0000000

Link to comment
Share on other sites

<pre>
<?php
$string = "Hello World and here is a longer string";
$n=0;
foreach(str_split($string,16) as $chunk){
  foreach(str_split($chunk,4) as $line){
    printf("%-4s = %s\n",$line,str_pad(bin2hex(strrev($line)),8,0,STR_PAD_LEFT));
    $n++;
    if(!($n%4)) print "\n";
  }
}
print str_repeat("     = 00000000\n",4-($n%4));
?>

Link to comment
Share on other sites

Thank You For All Your Help So Far. There's just 1 last thing i need with this script to be able to do. The ability to output the resulted hex on on arrary of addresses per say.

 

Something alike this.

 

[Ascii Name Here] - In This case It Will Be Hello World

0x00000004 0x6c6c6548

0x00000008 0x6f57206f

0x0000000C 0x00646c72

0x00000010 0x0000000

 

while still filling the remainder of whats left with zeros.

Link to comment
Share on other sites

So, you want the output to be like:

 

0x005A42AC 0x6C6C6548
0x005A42B0 0x6F57206F
0x005A42B4 0x00646C72
0x005A42B8 0x00000000
0x005A42BC 0x00000000
0x005A42C0 0x00000000
0x005A42C4 0x00000000
0x005A42C8 0x00000000

 

What is the first column?

Link to comment
Share on other sites

All that matters is the hex gets outputted in reverse order to the left of them like so.

 

If i type in hello World it should be outputted like this:

 

0x00010000 0x6c6c6548

0x00010004 0x6f57206f

0x00010008 0x00646c72

0x0001000C 0x00000000.

 

You already showed me how to convert text to hex. TY Now i need to output the result of that conversion like stated above. :)

Link to comment
Share on other sites

<pre>
<?php
$key = 5915308;
$key_incr = 4;
$format = "0x%08s 0x%08s\n";

$string = "Hello World";
$n=0;
foreach(str_split($string,16) as $chunk){
  foreach(str_split($chunk,4) as $line){
    printf($format,strtoupper(dechex($key)),strtoupper(bin2hex(strrev($line))));
    $n++;
    $key += 4;
  }
}
//Fill in extra empty rows
for($n = $n%4;$n && $n < 4;$n++){
  printf($format,strtoupper(dechex($key)),"00000000");
  $key += 4;
}
?>

Link to comment
Share on other sites

  • 2 weeks later...

OK guys just came back from vacation and im glad to see my post got answered by this wonderful user :)

 

Im having a little problem returning the non hex version of the inputted value ie the value the user inputted on the top of the code. any help with that?

 

Here is what im trying to do:

 

on top of the outputted hex values is a line that shows what the hex means

 

#[Original ascii here]

0x00000004 0x20202020

0x00000008 0x20202020

0x0000000c 0x20202020

0x00000010 0x20202020

 

I tried editing the format but it returned

 

#[Original ascii here]

0x00000004 0x20202020

#[Original ascii here]

0x00000008 0x20202020

#[Original ascii here]

0x0000000c 0x20202020

#[Original ascii here]

0x00000010 0x20202020

 

Instead of

 

#[Original ascii here]

0x00000004 0x20202020

0x00000008 0x20202020

0x0000000c 0x20202020

0x00000010 0x20202020

 

Please help me in fixing that :) Thank you all

-Brandon_R

Link to comment
Share on other sites

Hello could someone change the code

 

<pre>
<?php
$key = 5915308;
$key_incr = 4;
$format = "0x%08s 0x%08s\n";

$string = "Hello World";
$n=0;
foreach(str_split($string,16) as $chunk){
  foreach(str_split($chunk,4) as $line){
    printf($format,strtoupper(dechex($key)),strtoupper(bin2hex(strrev($line))));
    $n++;
    $key += 4;
  }
}
//Fill in extra empty rows
for($n = $n%4;$n && $n < 4;$n++){
  printf($format,strtoupper(dechex($key)),"00000000");
  $key += 4;
}
?>

 

to use sprintf instead?

 

Thank You

Brandon_r

Link to comment
Share on other sites

are you really asking that?

 

<pre>
<?php
$key = 5915308;
$key_incr = 4;
$format = "0x%08s 0x%08s\n";

$string = "Hello World";
$output = "$string\n";
$n=0;
foreach(str_split($string,16) as $chunk){
  foreach(str_split($chunk,4) as $line){
    $output .= sprintf($format,strtoupper(dechex($key)),strtoupper(bin2hex(strrev($line))));
    $n++;
    $key += 4;
  }
}
//Fill in extra empty rows
for($n = $n%4;$n && $n < 4;$n++){
  $output .= sprintf($format,strtoupper(dechex($key)),"00000000");
  $key += 4;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.