SV: selecting range of chars from a string?

M

mrfirmware

Guest
Is there a better way to extract a range of chars from a SystemVerilog
string object? I have a string that has a 3 char offset and an 8 char
value packed together like this:

string line = "124DEADBEEF"; // all values in hex

I'd like to split the string into two unsigned integers in the spirit
of this:

int unsigned offset = line[0..2];
int unsigned value = line[3:11];

However, I've only had success with a brute force for-loop like this:

for (int chr = 0; chr < 3; ++chr) begin : get_offset
offset = { offset, line[chr] };
end

for (int chr = 3; chr < 11; ++chr) begin : get_value
value = { value, line[chr] };
end

Thanks,

- Mark
 
On Oct 1, 12:52 pm, mrfirmware <mrfirmw...@gmail.com> wrote:
Is there a better way to extract a range of chars from a SystemVerilog
string object? I have a string that has a 3 char offset and an 8 char
value packed together like this:

string line = "124DEADBEEF"; // all values in hex

I'd like to split the string into two unsigned integers in the spirit
of this:

int unsigned offset = line[0..2];
int unsigned value = line[3:11];

However, I've only had success with a brute force for-loop like this:

for (int chr = 0; chr < 3; ++chr) begin : get_offset
offset = { offset, line[chr] };
end

for (int chr = 3; chr < 11; ++chr) begin : get_value
value = { value, line[chr] };
end

Thanks,

- Mark
I haven't tried it, but I wonder if $sscanf would work. Maybe
something like...

$sscanf(line.substr(0,2), "%x", offset);
$sscanf(line.substr(3,11), "%x", value);

David Walker
 
On Oct 2, 12:52 am, mrfirmware <mrfirmw...@gmail.com> wrote:
Is there a better way to extract a range of chars from a SystemVerilog
string object? I have a string that has a 3 char offset and an 8 char
value packed together like this:

string line = "124DEADBEEF"; // all values in hex

I'd like to split the string into two unsigned integers in the spirit
of this:

int unsigned offset = line[0..2];
int unsigned value = line[3:11];

However, I've only had success with a brute force for-loop like this:

for (int chr = 0; chr < 3; ++chr) begin : get_offset
offset = { offset, line[chr] };
end

for (int chr = 3; chr < 11; ++chr) begin : get_value
value = { value, line[chr] };
end

Thanks,

- Mark
Hi,
System verilog will have substr function.
By using this we can extract some portion of the string
see the following example.

string s;
initial begin
s = "SystemVerilog";
$display(s.substr(2, 5)); // Display: stem
 
On Oct 9, 4:43 am, vishnuprasa...@gmail.com wrote:
On Oct 2, 12:52 am, mrfirmware <mrfirmw...@gmail.com> wrote:



Is there a better way to extract a range of chars from a SystemVerilog
string object? I have a string that has a 3 char offset and an 8 char
value packed together like this:

string line = "124DEADBEEF"; // all values in hex

I'd like to split the string into two unsigned integers in the spirit
of this:

int unsigned offset = line[0..2];
int unsigned value = line[3:11];

However, I've only had success with a brute force for-loop like this:

for (int chr = 0; chr < 3; ++chr) begin : get_offset
offset = { offset, line[chr] };
end

for (int chr = 3; chr < 11; ++chr) begin : get_value
value = { value, line[chr] };
end

Thanks,

- Mark

Hi,
System verilog will have substr function.
By using this we can extract some portion of the string
see the following example.

string s;
initial begin
s = "SystemVerilog";
$display(s.substr(2, 5)); // Display: stem
Thanks to David and vishnuprasanth. substr() appears to be what I
needed.

- Mark
 

Welcome to EDABoard.com

Sponsor

Back
Top