'binary' value in managed string

  • Thread starter Thread starter richard.usenet
  • Start date Start date
R

richard.usenet

I want a 'binary' value to be included in a managed string. ie a binary
0x01 not its ascii equivalent of 49 decimal or 0x31 hexidecimal.

The following code converts the checksum unsigned char to it's ascii
value when using visual studio express 2005. What do I need to do to
force visual studio not to do the conversion? Thanks.

{
unsigned int checksum = 0;
unsigned char checksum_string[2];
String^ message;

for(int i=0; i<stuffed_message->Length; ++i)
{
checksum += stuffed_message;
}
checksum_string[0] = (unsigned char) ((checksum>>8)&0xff);
checksum_string[1] = (unsigned char) (checksum&0xff);

message += (unsigned char) checksum_string[0]) + (unsigned char)
checksum_string[1];
}
 
Thanks for the System::Char pointer.

Interestingly the uncommented line gives my desired effect. The
commented line gives the same result as previously.

Can anyone enlighten me?

{
unsigned int checksum = 0;
System::Char checksum_string[2];
System::String^ message;

for(int i=0; i<stuffed_message->Length; ++i)
{
checksum += stuffed_message;
}
checksum_string[0] = (checksum>>8)&0xff;
checksum_string[1] = checksum&0xff;

//message += checksum_string[0] + checksum_string[1];
message += "" + checksum_string[0] + checksum_string[1];

}

I think I may have answered my own question in looking at these two
lines again. The + in the commented line performs an arthimetic
addition on the 'Char' values which is why I get "156" and not "0156"
for the binary checksum (expressed as hex) 0x009C. And in the
uncommented line the empty string forces the + to be consider as a
string concatenation.

This still leaves me with when does a value get converted to a text
string and when does it remain it's original value?

The following also gives my desired result. Not that I think it sheds
any further light on it for me.

message += checksum_string[0];
message += checksum_string[1];

and

message = message + checksum_string[0] + checksum_string[1];
 
Instead of thinking the number is 'promoted' to a string think in terms
of method overloading.

There are 3 compiler defined methods:
string operator +( string,x, string y);
string operator +( object,x, string y);
string operator +( string,x, object y);

Effectively, all 3 do a concat( x.ToString(), y.ToString() ), where a
null is considered an emtpy string;
 
Back
Top