FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2007, 09:52 PM
mrfirmware
Guest
 
Posts: n/a
Default SV: selecting range of chars from a string?

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

Reply With Quote
  #2 (permalink)  
Old 10-03-2007, 07:46 AM
Guest
 
Posts: n/a
Default Re: selecting range of chars from a string?

On Oct 1, 12:52 pm, mrfirmware <[email protected]> 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

Reply With Quote
  #3 (permalink)  
Old 10-09-2007, 10:43 AM
Guest
 
Posts: n/a
Default Re: selecting range of chars from a string?

On Oct 2, 12:52 am, mrfirmware <[email protected]> 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

Reply With Quote
  #4 (permalink)  
Old 10-11-2007, 11:20 PM
mrfirmware
Guest
 
Posts: n/a
Default Re: selecting range of chars from a string?

On Oct 9, 4:43 am, [email protected] wrote:
> On Oct 2, 12:52 am, mrfirmware <[email protected]> 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

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
parameter range specification Verictor Verilog 1 03-27-2007 03:28 AM
random number in a predefined range SB Verilog 2 12-03-2006 07:19 PM
defining a range in Verilog kb33 Verilog 2 03-26-2006 10:08 PM
bit range select of a genvar Andy Peters Verilog 1 03-09-2005 04:04 AM
Re: Selecting wire or trireg at runtime Avrum Verilog 0 07-08-2003 07:45 PM


All times are GMT +1. The time now is 08:59 PM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved