gaza165 Posted March 5, 2009 Share Posted March 5, 2009 i am trying to represent an integer in hexadecimal form. i have written everything needed to output it. as we all know.. hex 10 - 15 is A-F in hex.... do i need to write mutlitple if functions to check through the array!! helpp??? Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/ Share on other sites More sharing options...
Psycho Posted March 5, 2009 Share Posted March 5, 2009 Sorry, but your "question" is not clear. You say you have created the code needed to convert an integer to a hexadecimal value. Then you ask "do i need to write mutlitple if functions to check through the array". What are you doing with the array? I guess I will assume that the array is filled with integer values that you want converted to hexadecimal. Do you want those values repopulated back into the array or just echo'd to the page? Since you didn't provide the code you created for converting the value I can't tell you how to utilize it with your array (however, I'm guessing you created something that was not needed - I'm sure there is an easy way in PHP to do the conversion automatically). Without knowing the structure of your array I can't be sure, but you would probably wwant to do something like this foreach ($array as $intValue) { echo conversionFunction($intValue); } Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/#findComment-777570 Share on other sites More sharing options...
kenrbnsn Posted March 5, 2009 Share Posted March 5, 2009 What's wrong with using the function dechex? Ken Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/#findComment-777573 Share on other sites More sharing options...
genericnumber1 Posted March 5, 2009 Share Posted March 5, 2009 I'm not sure what you mean... <?php echo base_convert('1234567890', 10, 16); ^ will change base.... <?php foreach($array as &$value) { $value = base_convert($value, 10, 16); } ^ will do it for each value in an array (yes, I know I could do a one-liner with an anonymous function and array_walk, this is actually readable). edit: or as stated, you can use dechex() instead of base_convert Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/#findComment-777575 Share on other sites More sharing options...
gaza165 Posted March 5, 2009 Author Share Posted March 5, 2009 Well for a uni project, i have to write my own function. I must admit to you all now that this is a program being written in C and i know this is a PHP forum but i didnt know where else to go. Basically i have got it to output the following... Say i input 255 as my integer it outputs the following... 0 0 0 15 15 but obviously 15 is F in hex so i need it to output. 0 0 0 F F Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/#findComment-777576 Share on other sites More sharing options...
sasa Posted March 5, 2009 Share Posted March 5, 2009 are you try this <?php $digits = array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F'); $hex_arr = array(1,12,5,13,15); foreach ($hex_arr as $d) echo $digits[$d]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148129-need-help-with-array-conversion/#findComment-777577 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.