Remove garbage data in byte[]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dantz
    New Member
    • Oct 2008
    • 71

    Remove garbage data in byte[]

    Hi,

    I am having difficulties on how I can remove all the "\0" (and all other garbage stuffs) in my variable.

    Code:
    byte[] mybuff = new byte[512];
    API_Read(handle, block, mybuff);
    After calling the API, mybuff will return something that I will convert to string by using this

    Code:
    string newStr = Encoding.ASCII.GetString(retbuff);
    However, since mybuff is having a big size but the content is just small, those unused space were filled with garbage data that I need to eliminate, to work on the string data properly.

    BTW, the 512 size is a requirement by the API, so I cannot use smaller sizes.

    Please help.

    I am using
    .NET 3.5 in Vista Business

    TIA

    dantz
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Can you initialize the byte array?

    Code:
    byte[] mybuff = new byte[512];
    for (int i = 0; i < mybuff.Length; i++)
      mybuff[i] = 0;
    Or does the garbage come after the call to the API?

    Comment

    • dantz
      New Member
      • Oct 2008
      • 71

      #3
      Thanks for your reply.
      Yes, the garbage come after the API call

      Comment

      • dantz
        New Member
        • Oct 2008
        • 71

        #4
        So far this is what I came up with. Is there any better solution than this?

        Code:
        byte[] mybuff = new byte[512];
        List<byte> listbuff = new List<byte>();
                    
        API_Read(handle, block, mybuff);
                       
        foreach (byte buff in mybuff)
        {
          if (buff == 0)
          {
             break;
          }
          listbuff.Add(buff);
        }
        
        byte[] resultbuff = listbuff.ToArray();
        string newStr = Encoding.ASCII.GetString(resultbuff);
        TIA

        dantz

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Can you post an example of the output returned by that API call? I did the following...

          Code:
                      char[] buff = "abcd\0lllllllll".ToCharArray();
                      byte[] byteBuff = new byte[buff.Length];
                      for (int i = 0; i < buff.Length; i++)
                          byteBuff[i] = (byte)buff[i];
          
                      string s = Encoding.ASCII.GetString(byteBuff);
          
                      Console.WriteLine(s);
          ... and got the expected result of "abcd" so I'm wondering what the API call is throwing at it. So your call to Encoding.ASCII. GetString should work, but it's not. I think we need to see what the input is.

          Comment

          • dantz
            New Member
            • Oct 2008
            • 71

            #6
            Hmm...a bit tough to copy and paste the data. When I use the text visualizer or message box to the string, it just return the correct string. But when I save it to file, the one below is what I get.

            Code:
            98989  oŽ    <      Tã- \µQo   (¯ ¨Ž     ±Á ¨Ž    tã- (¯ ¨Ž ÿÿÿÿP    µÁ P Ìã- hQoP }Qo‡FѪ¨Ž Lä-     èÔQ    0   0   €ê= èðQØã-     P ¨Ž     ä- ¨Ž Lä- ä- äQoä- ÆòVoËòVoWAѪÀé- Lä- €ê= €ê= P     *      Lä-     àã- Àì- ؝žo™³\«ˆÀ@ ˆ                  tä- R     ô Rì¾Qtä- À8PoÀé- ¨Ž R ì¾Q        $8˜6tN8€ä- û Œ¼Q˜ä- 8a¬QŒ¼Q    ü¨QŒ¼Q`å- ^¬QŒ¼Qðä- l«Q|«Q*>6    ü)8\t7    [XØb˜6        ÿÿÿÿŒ¼Q    $8            $8tN8
            I just captured the image instead...

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Is the desired result "98989"?

              Yea that will be hard to get into a program to test. My goal here was to take your input string and load it into a program, then see what I get on my own platform.

              You could always have your program output it as a byte array, then paste it here for me to import into a program. Something like...

              Code:
                          string str = "test str\nblah\0garbage";
                          int entriesPerLine = 10;
              
                          Console.WriteLine("byte[] buff = new byte[] {");
                          Console.Write("\t");
                          for (int i = 0; i < str.Length; i++)
                          {
                              Console.Write(((byte)str[i]).ToString("000"));
                              if (i != str.Length - 1)
                                  Console.Write(", ");
                              if (i != 0 && i % entriesPerLine == 0)
                              {
                                  Console.WriteLine();
                                  Console.Write("\t");
                              }
                          }
                          Console.WriteLine("};");

              Comment

              • dantz
                New Member
                • Oct 2008
                • 71

                #8
                Hi GaryTexmo,

                Sorry, I forgot to get back to this topic.

                Yes, the desired result "98989"

                Here are the output in bytes:
                176,185,81,129, 201,128,255,200 ,40,95,232,181, 43,143,255,91,5 2,157,234,235,1 03,185,88,167,1 7,97,146,122,17 2,50,220,172,92 ,252,191,27,220 ,94,63,249,97,2 4,151,83,180,22 8,102,98,71,175 ,90,229,127,189 ,98,10,168,228, 169,196,218,18, 189,197,85,217, 136,165,73,195, 48,187,74,97,24 0,178,138,195,1 20,187,15,181,2 04,104,252,104, 106,101,131,0,9 2,60,140,84,29, 15,139,43,151,1 2,214,137,119,0 ,111,37,208,107 ,188,29,137,153 ,187,116,173,11 1,15,177,129,14 8,246,116,99,32 ,64,119,130,141 ,62,83,0,53,171 ,51,52,13,65,55 ,46,134,136,94, 18,58,189,193,2 35,156,49,242,7 8,216,136,19,10 2,159,4,238,199 ,148,237,158,20 3,148,21,209,35 ,237,228,238,14 6,144,138,142,1 92,98,245,46,56 ,251,221,206,20 6,35,41,89,230, 250,227,64,42,1 80,43,179,4,79, 180,156,22,202, 58,130,151,124, 247,212,120,87, 43,2,151,61,54, 16,125,66,113,7 1,252,58,57,136 ,44,191,240,161 ,15,255,30,42,4 7,167,146,164,1 78,125,212,237, 133,151,35,136, 180,231,34,178, 202,116,206,162 ,90,50,94,19,56 ,115,158,164,18 9,214,148,221,1 36,192,85,254,6 7,24,224,18,84, 89,187,210,71,1 61,43,107,231,1 21,146,208,212, 125,235,55,125, 129,253,183,166 ,151,140,43,207 ,162,62,215,164 ,129,48,82,24,2 15,1,35,25,166, 12,22,38,125,11 ,64,186,102,76, 157,166,94,15,1 95,72,55,153,15 ,240,31,49,150, 218,76,108,229, 44,236,64,30,35 ,113,219,164,16 0,94,160,75,34, 129,248,55,74,3 1,57,255,215,13 7,94,205,232,86 ,43,112,246,184 ,192,22,131,255 ,205,151,224,89 ,98,237,38,54,9 9,71,247,244,10 7,234,220,246,1 4,178,110,201,4 9,66,130,186,21 7,117,22,129,20 1,26,225,136,15 5,108,122,164,1 43,3,197,56,230 ,112,7,9,62,253 ,54,253,202,68, 192,231,125,10, 125,35,148,84,8 6,155,177,105,1 44,187,113,67,2 12,46,185,158,1 67,153,64,106,2 26,254,247,17,2 24,243,175,160, 9,234,13,135,11 8,154,228,85,14 8,154,129,179,1 5,42,222,234,19 3,135,104,127,1 54,251,154,137, 150,118,132,111 ,30,60,163,196, 68,191,30,102,2 42,171,139,156, 173,173,116,15, 44,220,10,113,9 9,14,196,42,215 ,138,37,215,79, 79,40,117,156,1 29,

                Thanks!

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  Yea I'm seeing the same result... I'm not sure why that call stopped on a null character with the shorter string, but doesn't on a longer string.

                  Oh well, is there any reason you have to use that method call? You could fairly easily just run through the returned bytes and append them to a string (stringbuilder) until you hit a value of 0.

                  Will that work?

                  Comment

                  • dantz
                    New Member
                    • Oct 2008
                    • 71

                    #10
                    Yup, It can work for me (The post #4). As of now that is how I implement it. So far it seems okay, so I will be using this as of now (I just hope there would be no other issues may come in the future.lolz)

                    This is the only available method call that I need to use. Because it is an API from a hardware. So there is no choice but to really deal with it.

                    Thanks a lot for your time GaryTexmo. It was nice having a conversation with you.

                    Until next time. :)

                    - dantz

                    Comment

                    • HaLo2FrEeEk
                      Contributor
                      • Feb 2007
                      • 404

                      #11
                      I'm assuming this is happening because the data returned is in unicode format. I've read strings that were in unicode format from a file before and everytime I would get the propr string that I needed, then a lot of \0's at the end. My technique was to call Trim("\0") on the string. Since you have a lot of other garbage bytes, I would recommend looking for the first instance of \0 and timming from there to the end, which should leave you with your desired string. This is assuming, of course, that the first character after your desired result will always be \0.

                      Comment

                      • GaryTexmo
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1501

                        #12
                        Ah so you did (say that in #4) haha, I'd forgotten about this until you posted back. Anyway, yea I have no idea why it does that, sorry I couldn't help you further.

                        Halo2freek, this one is weird 'cause it takes the \0's and puts it right into the string, reading the entire byte array. I'm a bit confused myself... if you take the bytes he posted and bring them into a program, then do the calls on it, you'll see what's happening. Which is strange 'cause the example I posted in post #5 gives the expected behaviour, and it's exactly the same just with a shorter byte array :D

                        (at least, I think so)

                        Comment

                        Working...