how to concatenate string and int and assigned to a string;
ex:
module a ;
int a=5;
string name=“abc”;
string name2={name,a};
initial
begin
$display(“a=%d, name1=%s,name2=%s”,a,name,name2);
end
endmodule
output:a=5, name1=abc, name2=abc
//i nead the name2 = abc5, but i am getting name2=abc, why it it not working and any other possible way to concatenate?
In reply to sh88:
how to concatenate string and int and assigned to a string;
ex:
module a ;
int a=5;
string name=“abc”;
string name2={name,a};
initial
begin
$display(“a=%d, name1=%s,name2=%s”,a,name,name2);
end
endmodule
output:a=5, name1=abc, name2=abc
//i nead the name2 = abc5, but i am getting name2=abc, why it it not working and any other possible way to concatenate?
You can try using $formatf instead, look at the LRM section 21.3.3 Formatting data to a string
module a ;
int a=5;
string name="abc";
string name2;
initial
begin
name2 = $sformatf("%s%0d", name, a);
$display("a=%d, name1=%s,name2=%s",a,name,name2);
end
endmodule
# KERNEL: a= 5, name1=abc,name2=abc5
Or
module a ;
int a=5;
string name="abc";
string name2 = $sformatf("%s%0d", name, a);
initial
begin
$display("a=%d, name1=%s,name2=%s",a,name,name2);
end
endmodule
# KERNEL: a= 5, name1=abc,name2=abc5
HTH,
-R
In reply to rgarcia07:
BTW, it’s bad practice to have static variable initializations with dependancies on other static variable initializations. There is no defined ordering between initializations.
In reply to dave_59:
In reply to rgarcia07:
BTW, it’s bad practice to have static variable initializations with dependancies on other static variable initializations. There is no defined ordering between initializations.
Hi Dave,
Yes, thanks for pointing that out :)
Regards,
-R
In reply to dave_59:
hi Dave,
any other possible way?
In reply to sh88:
In reply to dave_59:
hi Dave,
any other possible way?
Many other ways. What is wrong with what is already shown?
In reply to dave_59:
Dave
can you explain with examples
In reply to sh88:
module a ;
int a;
string name;
string name2;
initial
begin
a = 5;
name = "abc";
// one way
name2 = $sformatf("%s%0d", name, a);
$display("a=%d, name1=%s,name2=%s",a,name,name2);
// another way
name2.itoa(a);
name2 = {name,name2};
$display("a=%d, name1=%s,name2=%s",a,name,name2);
// yet another way
$display("a=%d, name1=%s,name2=%s%0d",a,name,name,a);
end
endmodule
In reply to dave_59:
Thanks Dave