Rogerio Batista
Rogerio Batista

Reputation: 1

When i convert byte in string has a lot of unnecessary information c#

I need to convert this bytes in a string:

var smallDCWBytes = new byte[]
         {
              0xFE, 0x00, 0x00, 0xEF, 0xFF, 0xF8, 0x24, 0x16, 0x01, 0x04, 0x00, 0x00, 0x66, 0x37, 0x58, 0xCA,
              0xB8, 0x82, 0x00, 0x80, 0x53, 0x6D, 0x61, 0x6C, 0x6C, 0x20, 0x44, 0x43, 0x57, 0x20, 0x52, 0x65,
              0x63, 0x65, 0x69, 0x76, 0x65, 0x64
         };

var localDCWResponseText = System.Text.Encoding.ASCII.GetString(smallDCWBytes).Trim();

this is the answered that i received: "?\0\0???$\u0016\u0001\u0004\0\0f7X???\0?Small DCW Received"

I only need the "Small DCW Received", how is the better way to do this?

Upvotes: 0

Views: 155

Answers (1)

ArenaLor
ArenaLor

Reputation: 485

Actually it is doing what is requested, because it is converting every byte of your array. You can use:

var smallDCWBytes = new byte[]
     {
          0x53, 0x6D, 0x61, 0x6C, 0x6C, 0x20, 0x44, 0x43, 0x57, 0x20, 0x52, 0x65,
          0x63, 0x65, 0x69, 0x76, 0x65, 0x64
     };

var localDCWResponseText = System.Text.Encoding.ASCII.GetString(smallDCWBytes);

Or, if you are forced to use that array, you case do something like:

var smallDCWBytes = new byte[]
     {
          0xFE, 0x00, 0x00, 0xEF, 0xFF, 0xF8, 0x24, 0x16, 0x01, 0x04, 0x00, 0x00, 0x66, 0x37, 0x58, 0xCA,
          0xB8, 0x82, 0x00, 0x80, 0x53, 0x6D, 0x61, 0x6C, 0x6C, 0x20, 0x44, 0x43, 0x57, 0x20, 0x52, 0x65,
          0x63, 0x65, 0x69, 0x76, 0x65, 0x64
     };

var localDCWResponseText = System.Text.Encoding.ASCII.GetString(smallDCWBytes).Substring(21,18);

Upvotes: 1

Related Questions