Jump to content

Binary string to sc_bv


samyakjaroli

Recommended Posts

I am trying a convert a string to a bit vector but unable to do so.
Aim is to directly store the string as bit vector into Memory.


Part of code:

//<required libs declared>

//..//

string line;

sc_bv<32> Mem[256];

//..//

case 1: {
i=1;
ifstream mem_1("../mem_1.txt");
if (mem_1){
while (getline(mem_1,line)){
Mem= line;
i++ ;
}
mem_1.close();
}
else {
cout << "mem_1 not found  ";
}
 
break;
}

 

Where mem_1.txt contains binary: eg.

00100000000001000000000000000000
00000000010000110010100000100100
00000000011000000011000000100111
 
Error:
 
/usr/local/systemc-2.3.0/include/sysc/datatypes/bit/sc_bv.h:192:15: note: sc_dt::sc_bv<W>& sc_dt::sc_bv<W>::operator=(sc_dt::int64) [with int W = 32, sc_dt::int64 = long long int]
/usr/local/systemc-2.3.0/include/sysc/datatypes/bit/sc_bv.h:192:15: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘long long int’

 

 

Thanks in advance.

 

--

Samyak 

Link to comment
Share on other sites

Hello,

It appears that you are trying to store byte strings. Correct me if I am wrong.

As SystemC is a C++ library, why not store the strings being read in as 'unsigned

 

char' strings. In both C and C++, 'unsigned char' is byte data type. In cases

like these, I am afraid you would have to use good old C style processing

techniques. Hope that helps.

 

Link to comment
Share on other sites

Samyak,

 

in general, I like that you use istream and getline to read in the text file.  This is a well-proven pattern in C++.

Some remarks below.

 

sc_bv<32> Mem[256];

 

Why not use an std::vector here?  You can't know in advance, how many lines are in the file, do you?

std::vector< sc_bv<32> > Mem;
Mem.reserve(256); // we expect at least 256 entries

 

//..//

case 1: {
i=1;
 

 

You should probably start at zero here.  Otherwise, your first entry in Mem is not read from the file.

 

 

while (getline(mem_1,line)){
Mem= line;
i++ ;
}

 

You need to check the array bounds as well, otherwise you'll run into a stack overflow.

 

while( i<256 && getline(mem_1,line) ) {
  Mem[i] = line.c_str();
  i++;
}

Or you can use an std::vector and just read whatever is in the file.  You can still check, if a sufficient number of lines have been read.

while( getline(mem_1,line) ) {
  Mem.push_back( line.c_str() ); // append new bitvector
}
if( Mem.size() < 256 )
  // error

 

In both of the above examples, your original error has been fixed as well.  You can't construct an sc_bv from an std::string, but you can construct it from a plain C-style string.  You can obtain an equivalent C-style string from an std::string via the member function c_str().

 

Greetings from Oldenburg,

  Philipp

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...