Convert const string& to CByteArray ?
-
Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M
-
Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M
Try (I didn't test it...)
CString str = _T("Hi");
CByteArray arr;
int iSize = (str.GetLength()+1) * sizeof(TCHAR)
const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
for (i = 0; i < iSize; i++)
{
arr.Add(pByte[i]);
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Try (I didn't test it...)
CString str = _T("Hi");
CByteArray arr;
int iSize = (str.GetLength()+1) * sizeof(TCHAR)
const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
for (i = 0; i < iSize; i++)
{
arr.Add(pByte[i]);
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thanks for your reply. The code snippet works fine. But the snippet is for convertion of CString to CByteArray. But i'm looking for a const string conversion to CByteArray. Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop. Regards, Mayur M
-
Thanks for your reply. The code snippet works fine. But the snippet is for convertion of CString to CByteArray. But i'm looking for a const string conversion to CByteArray. Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop. Regards, Mayur M
mayur8u wrote:
But i'm looking for a const string conversion to CByteArray.
Are you talking about std::string?
mayur8u wrote:
Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop.
Yes, you may use
CByteArray
methodsSetSize
andGetdata
to copy the string content into the array buffer.CString str = _T("Hi");
CByteArray arr;
int iSize = (str.GetLength()+1) * sizeof(TCHAR)
arr.SetSize(iSize);
const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
CopyMemory( arr.GetData(), pByte, iSize);As usual, the test is up to you. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
mayur8u wrote:
But i'm looking for a const string conversion to CByteArray.
Are you talking about std::string?
mayur8u wrote:
Even if i use the CString conversion to CByteArray, is there any inbuilt methods rather than using the for loop.
Yes, you may use
CByteArray
methodsSetSize
andGetdata
to copy the string content into the array buffer.CString str = _T("Hi");
CByteArray arr;
int iSize = (str.GetLength()+1) * sizeof(TCHAR)
arr.SetSize(iSize);
const BYTE * pByte = (const BYTE *) (LPCTSTR) str;
CopyMemory( arr.GetData(), pByte, iSize);As usual, the test is up to you. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Ya! i'm talking about converting from std::string to CByteArray. If there is no solution then i might have to go to CString. (Last option)
std::string str("Hi");
int iLen = str.length();
CByteArray arr;
arr.SetSize(iLen);
CopyMemory( arr.GetData(), str.c_str(), iLen);Please note, the above code is not adding the null terminator at the tail of the array. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
std::string str("Hi");
int iLen = str.length();
CByteArray arr;
arr.SetSize(iLen);
CopyMemory( arr.GetData(), str.c_str(), iLen);Please note, the above code is not adding the null terminator at the tail of the array. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi, How do i convert a const string& to CByteArray ? I have to convert the string to CByteArray and pass the member functions GetData() and GetSize() to a method as parameters Is it possible or is there an alternative way of converting string to CByteArray. An example would be appreciated. Thanks in advance, Regards, Mayur M