这是一个小代码示例:
首先,一个从字符串创建字节动态数组的示例。动态字节数组包含每个字符的 ASCII 代码数字表示。优点是这可以是例如随机的,但字符串不能是随机的。
(创建做例如
for(i=0;i<stringvar.len(); i++) begin
byte_din_array = {byte_din_array ,stringvar[i]}; //stringvar[i] will return empty byte if the index would be beyond the string length
//The advantage of using stringvar[i] instead of stringvar.atoi(i) is that
//the string can have all ASCII characters and not just numbers.
//Disadvantage is that the byte contains the ASCII CODE "number"
//representation of the character and that is not human readable
end
)。
这是将动态字节数组转换回连接字符串的示例。您可能已经使用以前的动态数组在 xfer 中部分随机化(带有约束)或在 post_randomize 中更改。
function string convert_byte_array2string(byte stringdescriptionholder[]);
automatic string temp_str="";
automatic byte byte_temp;
automatic string str_test;
for ( int unsigned i = 0; i<stringdescriptionholder.size(); i++) begin
i=i;//debug breakpoint
byte_temp = stringdescriptionholder[i];
str_test = string'(byte_temp); //the "string cast" will convert the numeric ASCII representation in a string character
temp_str = {temp_str,str_test};
end
return temp_str;
endfunction