CString to CByteArray
-
Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:
CByteArray arrByte2; BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
// sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
arrByte2.SetSize(sSerial.GetLength());
memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.
-
Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:
CByteArray arrByte2; BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
// sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
arrByte2.SetSize(sSerial.GetLength());
memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.
It looks like the VS debugger is dumping out additional (or the allocated) memory. Is it for exactly the posted code? Or is
arrByte2
declared somewhere else and has been used before? You are also copyingm_sSerial.GetLength()
bytes while the array size has been set tosSerial.GetLength()
. Ifm_sSerial
is a longer string, you will have a buffer overrun. Finally, your casting should beconst BYTE* pByteArray = reinterpret_cast<const BYTE*>(sSerial.GetString());
That will at least keep the constness of the string but fail too for Unicode builds when
sSerial
is not explicitly declared asCStringA
. You might callarrByte2.GetSize()
and ignore the additional output if that is as expected. -
Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:
CByteArray arrByte2; BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
// sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
arrByte2.SetSize(sSerial.GetLength());
memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.
Flaviu2 wrote:
Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.
It does NOT have this trash "on tail". This trash is behind the arrByte2. Debugger interprets its content as characters and cannot find the actual "end" of array since you did not copy the terminated NULL from the CString.
-
Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:
CByteArray arrByte2; BYTE\* pByteArray = (PBYTE)(LPCTSTR)sSerial;
// sSerial is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
arrByte2.SetSize(sSerial.GetLength());
memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
// pByteArray is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=
// arrByte2 is jwKMTAABtm9D3CTrm6bmDRzVgxeQY66FZE4SQSjyPlCyjJK7v8ICQM2qIiQgafSb5PDubf8PDVa4n+b4CAbZdFEd+M6CzjHwRqje3DtGSeU=ýýýý««««««««these values, sSerial, pByteArray and arrByte2 are taken from VS debugger. Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ? Thank you for any hint.
You need to copy the length of the string + 1 to get the ending '\0' to terminate a C string :-) Very basic .. without it the string keeps going in C terms which is exactly what the debugger is showing with rubbish that was in the buffer.
In vino veritas
-
You need to copy the length of the string + 1 to get the ending '\0' to terminate a C string :-) Very basic .. without it the string keeps going in C terms which is exactly what the debugger is showing with rubbish that was in the buffer.
In vino veritas