Jump to content

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 

Share this post


Link to post
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.

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×