convert hex to index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

convert hex to index

Post by mikeeeeeeey »

Hi,

Where am I going wrong?

Code: Select all

 
$index = 0x $hex;
 
I know this is the wrong syntax for what I want to do, but I'm not sure how to write what I want to do. When I have $hex populated by a hexadecial such as #714e5f, I want to convert this to the index which is 7425631. doing...

Code: Select all

$index = 0x714e5f
works fine, but $hex is going to be constantly changing.....help!

Thanks for readin.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: convert hex to index

Post by Ambush Commander »

You want hex2dec(). The syntax you are attempting is not supported.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: convert hex to index

Post by jimthunderbird »

Guess this will be up to your requirement.

Code: Select all

 
<?php
  $hex_input = "714e5f";
  $command = "\$index=0x".$hex_input.";";
  eval($command);
  print $index;
?>
 
hex2dec() is good too.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: convert hex to index

Post by Ambush Commander »

I'm sorry, but I'm going to say, "Don't use that code." There is absolutely no reason why you need to bust out eval in this case.
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: convert hex to index

Post by jimthunderbird »

Ambush Commander wrote:I'm sorry, but I'm going to say, "Don't use that code." There is absolutely no reason why you need to bust out eval in this case.
Alright, then hex2dec() is good enough. I'm just thinking out loud.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: convert hex to index

Post by Kieran Huggins »

Someone here once said "if you need eval(), you're doing it wrong." I tend to agree!
Post Reply