Asked  7 Months ago    Answers:  5   Viewed   36 times

How can I convert this:

9312660682897061594767289296453011313180604726492752614962349778735928598708212386406555876878916185094145420038141655929349984786756296776268556142401047 

in base 16 ?

I found this recursive function:

function bcdechex($dec) {
            $last = bcmod($dec, 16);
            $remain = bcdiv(bcsub($dec, $last), 16);

            if($remain == 0) {
                return dechex($last);
            } else {
                return bcdechex($remain).dechex($last);
            }
        }

but the nesting level of 100 is reached, even if I have xdebug.max_nesting_level = 1000 in my php.ini config file.

My xdebug configuration looks like this:

;Xdebug
zend_extension = "${path}phpphp546x121216181946php_xdebug-2.2.1-5.4-vc9.dll"
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart = false
xdebug.dump_globals=1
xdebug.dump=COOKIE,FILES,GET,POST,REQUEST,SERVER,SESSION
xdebug.dump.SERVER=REMOTE_ADDR,REQUEST_METHOD,REQUEST_URI
xdebug.show_local_vars=1
xdebug.show_mem_delta=1
xdebug.collect_includes=1
xdebug.collect_vars=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.auto_trace=0
xdebug.trace_options=0
xdebug.trace_format=0
xdebug.trace_output_dir="${path}xdebugtrace"
xdebug.trace_output_name="trace.%t"
xdebug.profiler_enable=0
xdebug.profiler_append=1
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir="${path}xdebugprofiler"
xdebug.profiler_output_name="cachegrind.out.%s.%t"
xdebug.max_nesting_level = 1000
;/Xdebug

Does someone have a different function than this or any hint on how I can convert this integer number?

Thnak you!

 Answers

35

It's pretty easy to modify the function you found to be iterative rather than recursive:

function bcdechex($dec) {
    $hex = '';
    do {    
        $last = bcmod($dec, 16);
        $hex = dechex($last).$hex;
        $dec = bcdiv(bcsub($dec, $last), 16);
    } while($dec>0);
    return $hex;
}

Your example $bignum converted to hex is: b1cf5653e79bef001acfb0f99d1f34487d16a8253e3a9971e98d46382114e8ac81b5102ab3c56be1f77d0eb754f566c0dacb23d64755e823f35411f9e14c5617

Wednesday, March 31, 2021
 
Corne
answered 7 Months ago
37

Long.decode(str) accepts a variety of formats:

Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString:

  • Signopt DecimalNumeral
  • Signopt 0x HexDigits
  • Signopt 0X HexDigits
  • Signopt # HexDigits
  • Signopt 0 OctalDigits

Sign:

  • -

But in your case that won't help, your String is beyond the scope of what long can hold. You need a BigInteger:

String s = "4d0d08ada45f9dde1e99cad9";
BigInteger bi = new BigInteger(s, 16);
System.out.println(bi);

Output:

23846102773961507302322850521

For Comparison, here's Long.MAX_VALUE:

9223372036854775807

Wednesday, June 9, 2021
 
FWH
answered 5 Months ago
FWH
96

To convert from hex to decimal, there are many ways to do it in the shell or with an external program:

With bash:

$ echo $((16#FF))
255

with bc:

$ echo "ibase=16; FF" | bc
255

with perl:

$ perl -le 'print hex("FF");'
255

with printf :

$ printf "%dn" 0xFF
255

with python:

$ python -c 'print(int("FF", 16))'
255

with ruby:

$ ruby -e 'p "FF".to_i(16)'
255

with node.js:

$ nodejs <<< "console.log(parseInt('FF', 16))"
255

with rhino:

$ rhino<<EOF
print(parseInt('FF', 16))
EOF
...
255

with groovy:

$ groovy -e 'println Integer.parseInt("FF",16)'
255
Tuesday, June 15, 2021
 
khaverim
answered 5 Months ago
95

One possible solution:

import java.lang.StringBuilder;

class Test {
  private static final int sizeOfIntInHalfBytes = 8;
  private static final int numberOfBitsInAHalfByte = 4;
  private static final int halfByte = 0x0F;
  private static final char[] hexDigits = { 
    '0', '1', '2', '3', '4', '5', '6', '7', 
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  };

  public static String decToHex(int dec) {
    StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
    hexBuilder.setLength(sizeOfIntInHalfBytes);
    for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
    {
      int j = dec & halfByte;
      hexBuilder.setCharAt(i, hexDigits[j]);
      dec >>= numberOfBitsInAHalfByte;
    }
    return hexBuilder.toString(); 
  }

  public static void main(String[] args) {
     int dec = 305445566;
     String hex = decToHex(dec);
     System.out.println(hex);       
  }
}

Output:

1234BABE

Anyway, there is a library method for this:

String hex = Integer.toHexString(dec);
Saturday, July 3, 2021
 
Oshrib
answered 4 Months ago
48

How about this:

$romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$roman = 'MMMCMXCIX';
$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
echo $result;

which should output 3999 for the supplied $roman. It seems to work for my limited testing:

MCMXC = 1990
MM = 2000
MMXI = 2011
MCMLXXV = 1975

You might want to do some validation first as well :-)

Sunday, August 1, 2021
 
jbowman
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :