Trim problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have develop a smart card device reading and writing program, but I am
facing a problem that when I read the value from the smart card, I get "A19\0
\0\0\0", (actually I have store the value A19 into the card from sector 17 to
sector 24, 8 bytes long), I try to use Trim () function to cut the
unnecessary variable to get back the value A19, but it seen that the \0 is
not an null character. I have tried to trim “\0â€, but it is not success too.
So what should I do to trim out the unnecessary character?
 
This work for you?

string s = "A19\0\0\0\0";

string ns = s.TrimEnd('\0');

Console.WriteLine(ns);

Console.WriteLine("Len:" + ns.Length);
 
Excuse me,

I am just being curious. Isn't "\0" the null character?
I always thought it was. You mean I was wrong all the time?

Thanks.
 
Unlike in C, .Net strings do not rely on the null character (i.e. U+0000) to
signal end of string. The string class has a length property. However,
some classes in the framework (or your own) may walk the string char-by-char
and think the string is ended at first \0 char - so your mileage may vary
when imbedding \0 chars in strings. However, in terms of the String type,
null (\0) is no different from any other char.

string s = "ABC\0\0";
for ( int i = 0; i < s.Length; i++ )
{
char c = s;
Console.WriteLine("Char value:" + (int)c);
}
 
"\0" is a string with a single character (the null character) in it.
'\0' is the null character.

rossum



Excuse me,

I am just being curious. Isn't "\0" the null character?
I always thought it was. You mean I was wrong all the time?

Thanks.


The ultimate truth is that there is no ultimate truth
 
Thank you for your solution . i have solve this problem .

but i'm facing another different case when i read the card it give me the
value "A19\0!!\0\0\0" and when i use trimend('\0') it will return me
"A19\0!!" , and i get error code when i try to put TrimEnd('\0!!') .
how can i trim out this '!!' ? or i should write my own trim function ?
 
Well you need to figure out what the card will return consistently. If it
will always return xxx\0!!\0\0\0 then that is easy. Just get the first
three chars. If the first chars are variable and the last chars will always
be "\0!!\0\0\0" then just remove the last 6 chars and keep the rest. If
both are variable, then not sure how you will ever know what to do. You
need to find the pattern.
 
William Stacey said:
Well you need to figure out what the card will return consistently. If it will always return
xxx\0!!\0\0\0 then that is easy. Just get the first three chars. If the first chars are variable
and the last chars will always be "\0!!\0\0\0" then just remove the last 6 chars and keep the
rest. If both are variable, then not sure how you will ever know what to do. You need to find
the pattern.

It looks like a standard zero terminated C string;
If that is the case then everything after the first '\0' is leftover garbage.

Try this:

string s = "A19\0!!\0\0\0"
int idx = s.IndexOf('\0');
if (idx > 0)
s = s.Substring(0,ix);

Hope this helps.
 
Back
Top