473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Remove garbage data in byte[]

71
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. byte[] mybuff = new byte[512];
  2. API_Read(handle, block, mybuff);
  3.  
After calling the API, mybuff will return something that I will convert to string by using this

Expand|Select|Wrap|Line Numbers
  1. string newStr = Encoding.ASCII.GetString(retbuff); 
  2.  
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
Oct 26 '10 #1
11 11707
GaryTexmo
1,501 Expert 1GB
Can you initialize the byte array?

Expand|Select|Wrap|Line Numbers
  1. byte[] mybuff = new byte[512];
  2. for (int i = 0; i < mybuff.Length; i++)
  3.   mybuff[i] = 0;
Or does the garbage come after the call to the API?
Oct 26 '10 #2
dantz
71
Thanks for your reply.
Yes, the garbage come after the API call
Oct 26 '10 #3
dantz
71
So far this is what I came up with. Is there any better solution than this?

Expand|Select|Wrap|Line Numbers
  1. byte[] mybuff = new byte[512];
  2. List<byte> listbuff = new List<byte>();
  3.  
  4. API_Read(handle, block, mybuff);
  5.  
  6. foreach (byte buff in mybuff)
  7. {
  8.   if (buff == 0)
  9.   {
  10.      break;
  11.   }
  12.   listbuff.Add(buff);
  13. }
  14.  
  15. byte[] resultbuff = listbuff.ToArray();
  16. string newStr = Encoding.ASCII.GetString(resultbuff); 
  17.  
TIA

dantz
Oct 26 '10 #4
GaryTexmo
1,501 Expert 1GB
Can you post an example of the output returned by that API call? I did the following...

Expand|Select|Wrap|Line Numbers
  1.             char[] buff = "abcd\0lllllllll".ToCharArray();
  2.             byte[] byteBuff = new byte[buff.Length];
  3.             for (int i = 0; i < buff.Length; i++)
  4.                 byteBuff[i] = (byte)buff[i];
  5.  
  6.             string s = Encoding.ASCII.GetString(byteBuff);
  7.  
  8.             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.
Oct 26 '10 #5
dantz
71
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.

Expand|Select|Wrap|Line Numbers
  1. 98989  oŽ    <      T- \Qo   ( Ž      Ž    t- ( Ž P      P - hQoP }Qo‡FѪŽ L-     Q    0   0   €= Q-     P Ž     - Ž L- - Qo- VoVoWAѪ- L- €= €= P     *      L-     - - ؝žo™\ˆ@ ˆ                   t- R      RQt- 8Po- Ž R Q        $8˜6tN8€-  ŒQ˜- 8aQŒQ    QŒQ`- ^QŒQ- lQ|Q*>6    )8\t7    [Xb˜6        ŒQ    $8            $8tN8                                
  2.  
  3.  
I just captured the image instead...

Oct 27 '10 #6
GaryTexmo
1,501 Expert 1GB
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...

Expand|Select|Wrap|Line Numbers
  1.             string str = "test str\nblah\0garbage";
  2.             int entriesPerLine = 10;
  3.  
  4.             Console.WriteLine("byte[] buff = new byte[] {");
  5.             Console.Write("\t");
  6.             for (int i = 0; i < str.Length; i++)
  7.             {
  8.                 Console.Write(((byte)str[i]).ToString("000"));
  9.                 if (i != str.Length - 1)
  10.                     Console.Write(", ");
  11.                 if (i != 0 && i % entriesPerLine == 0)
  12.                 {
  13.                     Console.WriteLine();
  14.                     Console.Write("\t");
  15.                 }
  16.             }
  17.             Console.WriteLine("};");
Oct 27 '10 #7
dantz
71
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,14 3,255,91,52,157,234,235,103,185,88,167,17,97,146,1 22,172,50,220,172,92,252,191,27,220,94,63,249,97,2 4,151,83,180,228,102,98,71,175,90,229,127,189,98,1 0,168,228,169,196,218,18,189,197,85,217,136,165,73 ,195,48,187,74,97,240,178,138,195,120,187,15,181,2 04,104,252,104,106,101,131,0,92,60,140,84,29,15,13 9,43,151,12,214,137,119,0,111,37,208,107,188,29,13 7,153,187,116,173,111,15,177,129,148,246,116,99,32 ,64,119,130,141,62,83,0,53,171,51,52,13,65,55,46,1 34,136,94,18,58,189,193,235,156,49,242,78,216,136, 19,102,159,4,238,199,148,237,158,203,148,21,209,35 ,237,228,238,146,144,138,142,192,98,245,46,56,251, 221,206,206,35,41,89,230,250,227,64,42,180,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,71,252,58,57,136,44,1 91,240,161,15,255,30,42,47,167,146,164,178,125,212 ,237,133,151,35,136,180,231,34,178,202,116,206,162 ,90,50,94,19,56,115,158,164,189,214,148,221,136,19 2,85,254,67,24,224,18,84,89,187,210,71,161,43,107, 231,121,146,208,212,125,235,55,125,129,253,183,166 ,151,140,43,207,162,62,215,164,129,48,82,24,215,1, 35,25,166,12,22,38,125,11,64,186,102,76,157,166,94 ,15,195,72,55,153,15,240,31,49,150,218,76,108,229, 44,236,64,30,35,113,219,164,160,94,160,75,34,129,2 48,55,74,31,57,255,215,137,94,205,232,86,43,112,24 6,184,192,22,131,255,205,151,224,89,98,237,38,54,9 9,71,247,244,107,234,220,246,14,178,110,201,49,66, 130,186,217,117,22,129,201,26,225,136,155,108,122, 164,143,3,197,56,230,112,7,9,62,253,54,253,202,68, 192,231,125,10,125,35,148,84,86,155,177,105,144,18 7,113,67,212,46,185,158,167,153,64,106,226,254,247 ,17,224,243,175,160,9,234,13,135,118,154,228,85,14 8,154,129,179,15,42,222,234,193,135,104,127,154,25 1,154,137,150,118,132,111,30,60,163,196,68,191,30, 102,242,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,129,

Thanks!
Nov 11 '10 #8
GaryTexmo
1,501 Expert 1GB
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?
Nov 11 '10 #9
dantz
71
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
Nov 12 '10 #10
HaLo2FrEeEk
404 256MB
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.
Nov 12 '10 #11
GaryTexmo
1,501 Expert 1GB
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)
Nov 12 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Covello | last post by:
Hello, When the following perl script is executed: #!/usr/bin/env perl use strict; use diagnostics; use warnings; # Header bytes for different zip formats my $GZIP_HEADER =...
2
by: Tonya | last post by:
Hi, i wanted to know how i could resolve the following error message: An unhandled exception of type 'System.ArgumentException' occurred in system.dll Additional information: The data grid...
0
by: Jawahar Rajan | last post by:
All, I have a datagrid that I use in a ASP.net web page that loads customer data based on the selected customer ID Some Customers will have no data in the database (which is oracle 9.2) . So I...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
1
by: Chris LaJoie | last post by:
I am wondering if it's possible to convert an object or a struct to a byte array of the raw data. It's been a long time, but I seem to remember doing this in C++ at one point, and with very little...
4
by: Hans Nieser | last post by:
Hi, I'm pretty new to C#, and recently I have been experimenting with sockets. However, I can't find a nice way (I've been fiddling with for-loops to no avail) to parse the data that comes in...
1
by: Sharon | last post by:
I have 2 questions that are just the opposite to one another: (1) I need to read an image file (like bitmap, jpeg etc.) and to save only its data, I need to save his data in a raw data format,...
0
by: Dave | last post by:
Hi, I send int buffer address from unmanaged c++ code to unmanaged c++ code throge managed code and i get the byts order in the buffer in opposite order per int(4 byte) why???
0
by: Michel Esber | last post by:
Hello, Linux RedHat AS4 running DB2 V8 FP15. I have very large tables (100 Million+ rows), and I only need to keep 6 months of data online/available. The tables have a timestamp field. I...
1
by: sankothari2002 | last post by:
Recently we have connected one bluetooth device which receive data from our serial device. The bluetooth device further transmite this data to a Windows Mobile which is bluetooth enable. Baud rate of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.