I have a code snippet that strangely works one way on a fedora 6 machine and another way on a fedora 8 machine.

void Form1::SendToApp()
{
  vector<int> isciistream;
  vector<int> fontstream;
  cout<<"Entering Form1::SendToApp()"<<endl;
  isciistream=wwnd->OnOK();
  isciistream.push_back(32);
  char str[255]="";
  cout<<"Size of IsciiStream:"<<isciistream.size()<<endl;
  for (int i=0;i<(int)isciistream.size();i++)
  {
    sprintf(str,"%s%c",str,isciistream[i]);
    cout << "isciistream[i] :" << isciistream[i]<< " str:"<<str[i]<<endl;
  }

 QString temp=str;
const char *foo = temp.ascii();
cout<<"foo:"<<(int)foo[0]<<","<<(int)foo[1]<<","<<(int)foo[2]<<endl;
}

for eg. if isciistream.size=3
isciistream[0]=204
isciistream[1]=32
isciistream[2]=32

on a fedora 6 machine the output i get is
foo: -52,32,32
which is correct.
in a fedora 8 machine i get
foo:32,0,43
I'm not sure why...

any help is appreciated

Recommended Answers

All 2 Replies

> sprintf(str,"%s%c",str,isciistream);
1. Almost all C library functions have undefined behaviour when the source and destination overlap. You're reading from the string you're printing into.

2. QString temp=str;
> const char *foo = temp.ascii();
If you print out the bytes of str before this, are they OK?
Perhaps QString defaults to a wide char on F8

2. no..theyre messed up.
it says -52,0,0

1. BRILLIANT!

i used a temp variable for the sprint f and it worked!!!!!!!! thanks a ton!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.