locked
CSharp Detecting only Character from Char or Byte Array RRS feed

  • Question

  • Dear All,

    I started to look for Detecting only Character from Char or Byte Array.

    Let say, byte[] bArr = new byte[20];

    bArr = {0x00, 0x01,0x02};

    I want to change cArr to string like char[] cArr = System.Text.Encoding.UTF8.GetString(bArr).ToCharArray();

    String str = new String (cArr);

    I want to see only str = 123 but now the last NUL byte will come together below

    str = 123 NUL NUL NUL 

    I hope someone will advise me how to display only the desired character or remove unnecessary byte data.

    Thanks and best regards


    Friday, March 1, 2013 9:12 AM

Answers

  • Hi All,

    I got it below.

    str.Repalce("\0","");

    Best regards

    • Marked as answer by Mr Ko Ko Friday, March 1, 2013 9:20 AM
    Friday, March 1, 2013 9:20 AM

All replies

  • Hi All,

    I got it below.

    str.Repalce("\0","");

    Best regards

    • Marked as answer by Mr Ko Ko Friday, March 1, 2013 9:20 AM
    Friday, March 1, 2013 9:20 AM
  • Hi 

    0X00, 0X01, 0X02 are set to byte array . These are considered as ASCII value and will be converted to equivalent string (0X00 is null character).

    If you want 123, then use ASCII values of 123) like

    byte[] bArr = new byte[] { 49, 50, 51};
    char[] cArr = System.Text.Encoding.UTF8.GetString(bArr).ToCharArray();
    String str = new String(cArr);

    Open notepad.exe

    Press Alt + number to see equivalent values - like Alt + 48.

    Friday, March 1, 2013 9:23 AM