SV: "atohex" fails for strings representing more than 32 bits, if bits set above that bit

Runninq Questa 10.7c

See System Verilog code below. This runs “atohex” three times to convert three different strings of nine hex characters, each to a 36-bit vector (35:0).

The problem is that the most significant hex digit of the output bit vector (representing bits 35:32) is incorrect, if it has bits that should be nonzero.

 string    hex;
      bit[35:0] num;
      
      hex = "012345678";
      $display ("hex = %s",hex);
      num = hex.atohex();
      $display ("num = %x",num);
      
      hex = "812345678";
      $display ("hex = %s",hex);
      num = hex.atohex();
      $display ("num = %x",num);
      
      hex = "5FFFFFFFF";
      $display ("hex = %s",hex);
      num = hex.atohex();
      $display ("num = %x",num);

Here is the response from simulation: Note how the most significant hex digit (representing bits 35:32) is incorrect if the input hex digit has a bit set in the upper nibble. The first example has none of the bits set in the upper nibble, so it works. The last two examples fail since they have bits set in the upper nibble.

# hex = 012345678  
  # num = 012345678
  # hex = 812345678
  # num = 012345678
  # hex = 5FFFFFFFF
  # num = fffffffff

Can anyone tell my why “atohex” is behaving this way? atohex is supposed to work for strings longer than 8 characters (32 bits), correct?

Thanks for your help!

Matt

In reply to mtgavin:

The return type of
atohex()
is integer, which is a 32-bit signed type.

If you need to convert values larger than 32-bits, use $sscanf as the LRM says.

status = $sscanf(hex, "%h", num);

In reply to dave_59:

It works. Thanks Dave!