dslauter
dslauter

Reputation: 75

php convert hex to decimal not working

I am trying to convert a hexadecimal value of 0d0c140d2f3b to decimal. hexdec() and base_convert() are giving me a value of 14345527177019

the value should be 13 12 20 13 47 59

   private function convert_rtc($hex_rtc) {
        $rtc = hexdec($hex_rtc);
        return $rtc;

If I should be using a different function let me know. Thank you

Upvotes: 0

Views: 3378

Answers (2)

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this..

<?php
var_dump(hexdec("See"));
var_dump(hexdec("ee"));
// both print "int(238)"

var_dump(hexdec("that")); // print "int(10)"
var_dump(hexdec("a0")); // print "int(160)"
?>

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212412

Split your original value of 0d0c140d2f3b into blocks of two characters each (e.g. 0d 0c 14 0d 2f and 3b), then use hexdec() on each individual block

PHP's str_split() function should be useful here

EDIT

for example

function convert_rtc($hex_rtc) {
    $rtc = array_map(
        'hexdec',
        str_split($hex_rtc, 2)
    );
    return $rtc;
}

$hexString = '0d0c140d2f3b';

$result = convert_rtc($hexString);
var_dump($result);

Upvotes: 5

Related Questions