pally99 Posted April 19, 2013 Share Posted April 19, 2013 Hi I appreciate your help, the code is kinda simple actually but I'm having trouble The following code echo's the WRONG result a5611bcd::0::0::abccef01 I need this following result a5611bcd::fffafaff::fefffeef::abccef01 I suspect the issue is that those two middle values are too large for integer, I know about bcmath, but whats the best way to get correct results into INTEGER form.. must be integer form, basically I my variables below are closely named sorry about this... <?php $KEY = array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01); /*this is the model of how everything below should end up in $SKEY $KEY = array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01); */ //so I begin with this string $getSKEY and try to make it into the above model $getSKEY = "A5611BCDFFFAFAFFFEFFFEEFABCCEF01"; $SKEY = array( hexdec(substr($getSKEY,0,), hexdec(substr($getSKEY,8,16)), hexdec(substr($getSKEY,16,24)), hexdec(substr($getSKEY,24,32)) ); echo dechex($SKEY[0])."::". dechex($SKEY[1])."::". dechex($SKEY[2])."::". dechex($SKEY[3]) ; ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted April 19, 2013 Share Posted April 19, 2013 Why are you converting from the string into numbers and back into a string? echo implode("::", str_split($getSKEY, ); // possibly with strtolower() Quote Link to comment Share on other sites More sharing options...
pally99 Posted April 19, 2013 Author Share Posted April 19, 2013 (edited) Why are you converting from the string into numbers and back into a string? echo implode("::", str_split($getSKEY, ); // possibly with strtolower() Hi thanks for your help but I'm not sure this answers my question correctly, the echo is really more of a test I suppose I was just trying to make sure that my values were being stored inside the $SKEY array as array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01); this is why I was converting from a string back to numbers, I was trying to take the first 8 hexString characters and make them into a decimal for the first integer array etc I did not think they were being stored correctly and I still don't think they are? ok so to be much clear just take string as input: "A5611BCDFFFAFAFFFEFFFEEFABCCEF01" output: array containing four integers each with the following hex value array(0xA5611BCD,0xFFFAFAFF,0xFEFFFEEF,0xABCCEF01); Edited April 19, 2013 by pally99 Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 19, 2013 Share Posted April 19, 2013 If you try to output hex values, they will show up as decimals in your browser. echo 0xA5611BCD; If you look at the documentation for dechex(), it actually returns a string, not a number. A number is the same number no matter what base you convert it to. If you actually want the hex string, requinix's solution gives you the exact output you asked for in your first post. If you want the integer values of each hex string, just run hexdec() one time. $strings = str_split($getSKEY, ; foreach ($strings as $string) echo hexdec($string).'<br/>'; I think the answer to your question relies wholly on what you are trying to accomplish. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 19, 2013 Share Posted April 19, 2013 (edited) Okay. Without actually checking it I think the problem is your use of substr(). hexdec(substr($getSKEY,0,), hexdec(substr($getSKEY,8,16)), hexdec(substr($getSKEY,16,24)), hexdec(substr($getSKEY,24,32))The third argument is a length, not an offset. Edited April 19, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Solution pally99 Posted April 19, 2013 Author Solution Share Posted April 19, 2013 If you try to output hex values, they will show up as decimals in your browser. echo 0xA5611BCD; If you look at the documentation for dechex(), it actually returns a string, not a number. A number is the same number no matter what base you convert it to. If you actually want the hex string, requinix's solution gives you the exact output you asked for in your first post. If you want the integer values of each hex string, just run hexdec() one time. $strings = str_split($getSKEY, ; foreach ($strings as $string) echo hexdec($string).'<br/>'; I think the answer to your question relies wholly on what you are trying to accomplish. lemminThank you for your help this was not correct requinix Thank you for your help but also not correct CORRECT ANSWER: I'm an idiot and used substr wrong! AHHHHHHHH... but thank you guys so much for being patient and offering very good suggestions I really appreciate it! 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.