Friday, March 02, 2012

String data types

String Datatype

System Verilog includes a string datatype, which is a variable size, dynamically allocated array of bytes.

SystemVerilog also includes a number of special methods to work with strings

string my Name = "Kacper";
string s1 = "hello";

String Operators:

len():

function int len()str.len() returns the length of the string, i.e., the number of characters in the string.
- If str is "", then str.len() returns 0.

putc():

task putc(int i, string s)
task putc(int i, byte c)
- str.putc(i, c) replaces the ith character in str with the given integral value.
- str.putc(i, s) replaces the ith character in str with the first character in s.

getc():

function int getc(int i)
- str.getc(i) returns the ASCII code of the ith character in str.

toupper():

function string toupper()
- str.toupper() returns a string with characters in str converted to uppercase.
- str is unchanged.

tolower():

function string tolower()
- str.tolower() returns a string with characters in str converted to lowercase.
- str is unchanged.

compare()

function int compare(string s)
- str.compare(s) compares str and s, as in the ANSI C strcmp function (with regard to lexical ordering and return value), and embedded null bytes are included.

icompare()

function int icompare(string s)
- str.icompare(s) compares str and s, like the ANSI C strcmp function, but the comparison is case insensitive and embedded null bytes are included.

substr()

function string substr(int i, int j)
- str. substr(i, j) returns a new string that is a substring formed by characters in position i through j of str.

atoi(), atohex(), atooct(), atobin()
function integer atoi()
function integer atohex()
function integer atooct()
function integer atobin()
- str. atoi() returns the integer corresponding to the ASCII decimal representation in str.
str = "123";
int i = str. atoi(); // assigns 123 to i.

The conversion scans all leading digits and underscores characters (_) and stops as soon as it encounters any other character, or the end of the string. Returns zero if no digits were encountered. It does not parse the full syntax for integer literals.

atohex interprets the string as hexadecimal.
atooct interprets the string as octal.
atobin interprets the string as binary.

atoreal()

function real atoreal()
- str.atoreal() returns the real number corresponding to the ASCII decimal representation in str.

The conversion parses Verilog syntax for real constants. The scan stops as soon as it encounters any character that does not conform to this syntax, or the end of the string. Returns zero if no digits were encountered.

itoa():

task itoa(integer i)
- str.itoa(i) stores the ASCII decimal representation of i into str (inverse of atoi).

hextoa():

task hextoa(integer i)
- str.hextoa (i) stores the ASCII hexadecimal representation of i into str (inverse of atohex).

bintoa():

task bintoa(integer i)
- str.bintoa(i) stores the ASCII binary representation of i into str (inverse of atobin).

realtoa():

task realtoa(real r)
- str.realtoa(r) stores the ASCII real representation of i into str (inverse of atoreal).

No comments:

Post a Comment

Popular Posts