pally99 Posted April 14, 2013 Share Posted April 14, 2013 Hi Thanks for your help I'll just describe what I want to do any advice or if you could provide your own example it would really help, thanks again! I have a string holding hex numbers "41495ab3edde720c" and I want to get those values out of the string and represent them as hexadecimal thus 0x41495ab3edde720c, then I want to xor that hex with another hex like 0x41495ab3edde720c ^ 0x1111111111111111 then I want to take the result which will be in this example (50584BA2FCCF631D) and turn it back into a string "50584BA2FCCF631D" I'm not that good with php it seems hard to do this easily? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 14, 2013 Share Posted April 14, 2013 (edited) XOR each digit with 1. While you treat it as a string. $hex = "41495ab3edde720c"; $chars = "0123456789abcdefABCDEF"; for ($i = 0, $len = strlen($hex); $i < $len; $i++) { $hex[$i] = $chars[strpos($chars, $hex[$i]) ^ 1]; }[edit] If you have to XOR with anything other than 1 then you can't use the $chars that has both lower- and uppercase letters. Reason being that with just 1 the before and after numbers won't cross the 0-9/a-f boundary (which is broken for 0-9/A-F) but with anything more it's possible it would. Edited April 14, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
pally99 Posted April 14, 2013 Author Share Posted April 14, 2013 XOR each digit with 1. While you treat it as a string. $hex = "41495ab3edde720c"; $chars = "0123456789abcdefABCDEF"; for ($i = 0, $len = strlen($hex); $i < $len; $i++) { $hex[$i] = $chars[strpos($chars, $hex[$i]) ^ 1]; }[edit] If you have to XOR with anything other than 1 then you can't use the $chars that has both lower- and uppercase letters. Reason being that with just 1 the before and after numbers won't cross the 0-9/a-f boundary (which is broken for 0-9/A-F) but with anything more it's possible it would. Oh well thank you for your response but yeah I should have mentioned I need to xor any two possible values I was just giving 1's as an example :/ Quote Link to comment Share on other sites More sharing options...
requinix Posted April 14, 2013 Share Posted April 14, 2013 (edited) Then you might as well go with simpler code instead of shortcuts. If you have the GMP extension installed or installable, that has gmp_xor. Otherwise roll your own. You can break the number down into chunks of, say, 6, and use base_convert() to get something faster than trying to do it one character at a time. function xorhex($hex1, $hex2) { $len = max(strlen($hex1), strlen($hex2)); $hex1 = str_pad($hex1, $len, "0", STR_PAD_LEFT); $hex2 = str_pad($hex2, $len, "0", STR_PAD_LEFT); $xor = ""; for ($i = 0; $i < $len; $i += 6) { $one = (int)base_convert(substr($hex1, $i, 6), 16, 10); $two = (int)base_convert(substr($hex2, $i, 6), 16, 10); $xor .= str_pad(base_convert($one ^ $two, 10, 16), 6, "0", STR_PAD_LEFT); } return ltrim($xor, "0"); }[edit] Okay, one shortcut. Only with bases that are a power of two (binary, octal, hexadecimal, not decimal) can you xor pieces of the numbers together. If the input strings were in base 10 then you'd have to XOR the entire number together. Or convert it to binary/octal/hex, XOR, and convert back. Edited April 14, 2013 by requinix 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.