Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

'binary' value in managed string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
richard.usenet@gmail.com - 18 May 2006 09:49 GMT
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[i];
    }
    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];
}
code@martinsound.com - 18 May 2006 17:43 GMT
Use System::Char for the checksum.
richard.usenet@gmail.com - 19 May 2006 04:27 GMT
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[i];
       }
       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];
code@martinsound.com - 19 May 2006 19:09 GMT
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;

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.