Jump to content

Need Help With Array Conversion!


gaza165

Recommended Posts

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???

 

 

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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