How does atoi works in SV?


module test();
  string str;
  string color;

  initial begin
    str = "zincronium";
    color = "Red";
    $display("a2i[1]| %s : %0d", str, str.atoi());
    $display("a2i[2]| %s : %0d", color, color.atoi());
  end
endmodule

Output

a2i[1]| zincronium : z
a2i[2]| Red : 0

Why the output a2i[1] shows “z” and why the output a2i[2] shows “0”? How exactly does atoi() works on alphabet strings?

The conversion scans all leading digits and underscore characters ( _ ) and stops as soon as it encounters any other character or the end of the string. It returns zero if no digits were encountered.

Both str.atoi() and color.atoi() should return 0, so your simulator has a bug. The atoi function returns an integer type, whose bits can be either 0, 1, x, or z. So that might be why it returned z rather than 0.

In reply to sbellock:

Hi sbellock,

Thank you for explaining the implementation of atoi(). I tried to run this code on EDA Playground using both VCS as well as Incisive simulators, but still I got the same outputs. I am still not able to figure out why that particular string (i.e zincronium) is showing the output ‘z’.

In reply to jaysipani:

In reply to sbellock:
Hi sbellock,
Thank you for explaining the implementation of atoi(). I tried to run this code on EDA Playground using both VCS as well as Incisive simulators, but still I got the same outputs. I am still not able to figure out why that particular string (i.e zincronium) is showing the output ‘z’.

I just copy and pasted your code into EDA Playground and I get

a2i[1]| zincronium : 0
a2i[2]| Red : 0

on the Big S and Big C simulators, which is correct.

In reply to sbellock:

In reply to jaysipani:
I just copy and pasted your code into EDA Playground and I get

a2i[1]| zincronium : 0
a2i[2]| Red : 0

on the Big S and Big C simulators, which is correct.

I think, I might had missed something while running the simulation on EDA playground and also I forgot to save the code. But I ran the simulation again and the outputs were same as what you got. Sorry for the inconvenience.