Trying to Disable string operator=
-
Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine
// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}Here is the piece of code that calls it
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)
here is the definition
BYTE statementnumber[4];
the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string
struct source
{
string stmt;
string loc;
string addr1;
string addr2;that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility
is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine
template
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
// assign _New_val to _Val, return previous _Val
_Ty _Old_val = static_cast<_Ty&&>(_Val);
_Val = static_cast<_Other&&>(_New_val);
return _Old_val;
} -
Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine
// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}Here is the piece of code that calls it
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)
here is the definition
BYTE statementnumber[4];
the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string
struct source
{
string stmt;
string loc;
string addr1;
string addr2;that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility
is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine
template
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
// assign _New_val to _Val, return previous _Val
_Ty _Old_val = static_cast<_Ty&&>(_Val);
_Val = static_cast<_Other&&>(_New_val);
return _Old_val;
}Since one of the constructors for
std::string
isstd::string(const char *, size_t count)
you can do your assignment using
unsigned char datum[6]{ 'A', 'B', 'C', 'D', 'E', 'F' }; // a non-nul terminated sequence of bytes
std::string str(reinterpret_cast(datum), 4); // assuming we just want the first 4 bytes from datumSince you're getting a read exception, that suggests to me that given
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4);
then probably your sourcrecordpointer is invalid.
Keep Calm and Carry On
-
Since one of the constructors for
std::string
isstd::string(const char *, size_t count)
you can do your assignment using
unsigned char datum[6]{ 'A', 'B', 'C', 'D', 'E', 'F' }; // a non-nul terminated sequence of bytes
std::string str(reinterpret_cast(datum), 4); // assuming we just want the first 4 bytes from datumSince you're getting a read exception, that suggests to me that given
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4);
then probably your sourcrecordpointer is invalid.
Keep Calm and Carry On
-
Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine
// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}Here is the piece of code that calls it
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)
here is the definition
BYTE statementnumber[4];
the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string
struct source
{
string stmt;
string loc;
string addr1;
string addr2;that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility
is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine
template
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
// assign _New_val to _Val, return previous _Val
_Ty _Old_val = static_cast<_Ty&&>(_Val);
_Val = static_cast<_Other&&>(_New_val);
return _Old_val;
}ForNow wrote:
I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string
I think it is not a good idea to consider a byte array as a string. It is only possible if you are 100% sure there is no any NULL (0x00) byte within this byte array, otherwise you string would represent only the part of the array up to the first NULL byte.
-
Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine
// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}Here is the piece of code that calls it
returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)
here is the definition
BYTE statementnumber[4];
the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string
struct source
{
string stmt;
string loc;
string addr1;
string addr2;that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path
C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility
is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine
template
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
// assign _New_val to _Val, return previous _Val
_Ty _Old_val = static_cast<_Ty&&>(_Val);
_Val = static_cast<_Other&&>(_New_val);
return _Old_val;
}I compiled, with Visual C++ 2022, and run (successfully) the following code:
#include using namespace std;
typedef unsigned char BYTE;
struct source
{
string stmt;
string loc;
string addr1;
string addr2;
};// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}int main()
{BYTE statementnumber[4];
source * returnsource = new source;
returnsource->stmt = convertToString(statementnumber, 4);
delete returnsource;
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
I compiled, with Visual C++ 2022, and run (successfully) the following code:
#include using namespace std;
typedef unsigned char BYTE;
struct source
{
string stmt;
string loc;
string addr1;
string addr2;
};// converts character array
// to string and returns it
string convertToString(unsigned char* a, int size)
{
int i;
string s = "";
for (i = 0; i < size; i++) {
s += a[i];
}
return s;
}int main()
{BYTE statementnumber[4];
source * returnsource = new source;
returnsource->stmt = convertToString(statementnumber, 4);
delete returnsource;
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
ForNow wrote:
I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string
I think it is not a good idea to consider a byte array as a string. It is only possible if you are 100% sure there is no any NULL (0x00) byte within this byte array, otherwise you string would represent only the part of the array up to the first NULL byte.
That’s a good idea when I read the byte array for any value between 0x00 and 0x09 I’ll add 48 Those values between 0x0a and 0x0f I’ll add 55 After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
-
That’s a good idea when I read the byte array for any value between 0x00 and 0x09 I’ll add 48 Those values between 0x0a and 0x0f I’ll add 55 After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
ForNow wrote:
After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
If you mean an std::string then you you don't have to add a terminating null character.
-
ForNow wrote:
After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?
If you mean an std::string then you you don't have to add a terminating null character.