ch2-String

2019-10-24

How are strings represented in Verilog ?

​ A single ASCII character requires 8-bits (1 byte) and to store a string we would need as many bytes as there are number of characters in the string.

Click here to learn about Verilog Arrays

1
2
3
4
5
reg  [16*8-1:0]   my_string;           // Can store 16 characters

my_string = "How are you"; // 5 zeros are padded from MSB, and 11 char are stored
my_string = "How are you doing?" // 19 characters; my_string will get " are you doing?"

Basic String Methods

​ SystemVerilog also includes a number of special methods to work with strings, which use built-in method notation.

Usage Definition Comments
str.len() function int len() Returns the number of characters in the string
str.putc() function void putc (int i, byte c); Replaces the ith character in the string with the given character
str.getc() function byte getc (int i); Returns the ASCII code of the ith character in str
str.tolower() function string tolower(); Returns a string with characters in str converted to lowercase
str.compare(s) function int compare (string s); Compares str and s, as in the ANSI C stcmp function
str.icompare(s) function int icompare (string s); Compares str and s, like the ANSI C strcmp function
str.substr (i, j) function string substr (int i, int j); Returns a new string that is a substring formed by characters in position i through j of str

String Conversion Methods

(先存下,万一用上了呢)

str.atoi() function integer atoi(); Returns the integer corresponding to the ASCII decimal representation in str
str.atohex() function integer atohex(); Interprets the string as hexadecimal
str.atooct() function integer atooct(); Interprets the string as octal
str.atobin() function integer atobin(); Interprets the string as binary
str.atoreal() function real atoreal(); Returns the real number corresponding to the ASCII decimal representation in str
str.itoa(i) function void itoa (integer i); Stores the ASCII decimal representation of i into str
str.hextoa(i) function void hextoa (integer i); Stores the ASCII hexadecimal representation of i into str
str.octtoa(i) function void octtoa (integer i); Stores the ASCII octal representation of i into str
str.bintoa(i) function void bintoa (integer i); Stores the ASCII binary representation of i into str
str.realtoa(r) function void realtoa (real r); Stores the ASCII real representation of r into str

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!