tags:

views:

89

answers:

5

dear i need help for change a character in php. i get some code from web:

char dest='a';
int conv=(int)dest;

can i use this code for convert character into numeric? or do you have any idea? i'm just want to show result as decimal number:

if null == 0
if A == 1
+2  A: 

This may help you: http://www.php.net/manual/en/function.ord.php

fcingolani
A: 

So, if you need the ASCII code you will need to do:

$dest = 'a';
$conv = ord($dest);

If you want something like:

a == 1
b == 2
.
.
.

you should do:

$dest = 'a';
$conv = ord($dest)-96;

For more info on the ASCII codes: http://www.asciitable.com/

And for the function ord: http://www.php.net/manual/en/function.ord.php

vfn
That's no php...
Jasper
can i use this code for get result as a decimal number?
klox
Variables don't have types in php. Should be `$dest = 'a'; $conv = ord($dest);`.
Brendan Long
@Jasper, fixed the syntax now!
vfn
@Klox, yes you can use it to get a decimal.
vfn
+2  A: 

It does indeed work as in the sample, except that you should be using php syntax (and as a sidenote: the language that code you found most probably was, it did not do the same thing).

So:

$in = "123";
$out = (int)$in;

Afterwards the following will be true:

$out === 123
Jasper
Looks like they're trying to get the ASCII value, but it's hard to tell..
Brendan Long
is this not reversed? I wish if null mean zero and if I type A the result is 1
klox
@klox: please rephrase that, I have no clue what you are trying to say...
Jasper
@Brendan Long: I interpreted `i'm just need standard number like 0,1,2,3` as him saying he wanted this, but as you say it's not clear. I added a result to my answer so it's clear what this does.
Jasper
+2  A: 

Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....

Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.

To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.

PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.

Live Example

<?php

    function toNumber($dest)
    {
        if ($dest)
            return ord(strtolower($dest)) - 96;
        else
            return 0;
    }

      // Let's test the function...        
    echo toNumber(NULL) . " ";
    echo toNumber('a') . " ";
    echo toNumber('B') . " ";
    echo toNumber('c');

      // Output is:
      // 0 1 2 3
?>

PS: You can look at the ASCII values here.

Peter Ajtai
Doesn't handle upper case correctly, but seems to be on the right track.
deceze
@deceze - That's because upper and lower have different ASCII values. I edited the answer so that upper case is handled the same way as lower case.
Peter Ajtai
how if null? i want if null == 0
klox
Peter Ajtai
A: 

It's very hard to answer because it's not a real question but just a little bit of it.
But if you ask.
It seems you need some translation table, that defines links between letters and numbers

A -> 2
B -> 3
C -> 4
S -> 1

or whatever.
You can achieve this by using an array, where keys would be these letters and values - desired numbers.

$defects_arr = array(
    'A' -> 2,
    'B' -> 3,
    'C' -> 4'
    'S' -> 1
};

Thus, you can convert these letters to numbers

$letter = 'A';
$number = $defects_arr($letter);
echo $number; // outputs 1

But it still seems is not what you want.
Do these defect types have any verbose equivalents? If so, why not to use them instead of letters?

Telling the whole story instead of little bit of it will help you to avoid mistakes and will save a ton of time, both yours and those who to answer.

Col. Shrapnel