Jump to content

Need Help With Array Conversion!


gaza165

Recommended Posts

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);
}

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.