Brandon_R Posted July 2, 2009 Share Posted July 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 $string = "Hello World"; print implode("\n",array_map(create_function('$a','return str_pad(bin2hex(strrev($a)),8,0);'),array_pad(str_split($string,4),4,''))); Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 <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)); ?> Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 i don't understand Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 I dont think im allowed to post links so i pmed you it. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 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? Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 The first column is an array of addresses that the converted hex will be printed onto. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 ok, where do these addresses come from? Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 I choose them. It doesn't matter, it could be different if you like. Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 2, 2009 Author Share Posted July 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 3, 2009 Author Share Posted July 3, 2009 Just checking to see if anyone has a solution for this. Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 3, 2009 Author Share Posted July 3, 2009 Still checking, read my provious posts. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 6, 2009 Share Posted July 6, 2009 <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; } ?> Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 16, 2009 Author Share Posted July 16, 2009 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 Quote Link to comment Share on other sites More sharing options...
Andy-H Posted July 16, 2009 Share Posted July 16, 2009 You found a tutorial on reverse engineering I take it... Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 16, 2009 Author Share Posted July 16, 2009 No i haven't sir, could you could take the time to point me to one please? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted July 16, 2009 Share Posted July 16, 2009 I sent the link in a PM. Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 16, 2009 Author Share Posted July 16, 2009 Thanks but still doesn't help me with the PHP aspect of it. Thanks For Trying Andy -Brandon_R Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 16, 2009 Author Share Posted July 16, 2009 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 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; } ?> Quote Link to comment Share on other sites More sharing options...
Brandon_R Posted July 16, 2009 Author Share Posted July 16, 2009 Once again thank you i almost had it Has = instead of .= -Brandon_R Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.