911: byte [] manipulation

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

Guest

HI all,
I am a newbie to C#....
I have a byte[] of variable length. Starting from byte[15] the array, i
have the following content in the array:

[username\0password\0info\01234]

How can I extract those string from the byte[]?

Hope someone can give me some advice.

Thanks.

Martin
 
There are several ways to accomplish this. U can use StringBuilder to append
bytes u read from the array.

Or you can convert byte array to a string.
System.Text.Encoding.Default.GetString method...

or too much solutions...
 
Hi,

you can use one of the XXXEncoding ( ASCIIEncoding , UTF8Encoding )
GetString method.


Cheers,
 
But since the string comes in at the 15 bytes of the array and end by a
\0...and the other string start right after the prior \0, how could i
effectively pass the content between those into a stringbuilder?

Yunus Emre ALPÖZEN said:
There are several ways to accomplish this. U can use StringBuilder to append
bytes u read from the array.

Or you can convert byte array to a string.
System.Text.Encoding.Default.GetString method...

or too much solutions...

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

Martin said:
HI all,
I am a newbie to C#....
I have a byte[] of variable length. Starting from byte[15] the array, i
have the following content in the array:

[username\0password\0info\01234]

How can I extract those string from the byte[]?

Hope someone can give me some advice.

Thanks.

Martin
 
using System;
using System.Text;

namespace SampleApplication
{
class Test
{
static byte[] CreateByteArray()
{
return
Encoding.Default.GetBytes("012345678901234username\0password\01234");
}
static void Main()
{
byte[] buffer = CreateByteArray();
///
/// Username has a length at 8
///
Console.WriteLine(
Encoding.Default.GetString(buffer,15,8));
Console.WriteLine();

///
/// Or You Don't know length
///
StringBuilder sb = new StringBuilder();
int index = 15;
while(index<buffer.Length)
{
if (buffer[index]=='\0' )
{
if (sb.Length>0)
{
Console.WriteLine(sb.ToString());
sb.Remove(0,sb.Length);
}
}
else
sb.Append((char)buffer[index]);
index++;
}
if (sb.Length>0)
Console.WriteLine(sb.ToString());
Console.Read();
}
}
}

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

Martin said:
But since the string comes in at the 15 bytes of the array and end by a
\0...and the other string start right after the prior \0, how could i
effectively pass the content between those into a stringbuilder?

Yunus Emre ALPÖZEN said:
There are several ways to accomplish this. U can use StringBuilder to
append
bytes u read from the array.

Or you can convert byte array to a string.
System.Text.Encoding.Default.GetString method...

or too much solutions...

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

Martin said:
HI all,
I am a newbie to C#....
I have a byte[] of variable length. Starting from byte[15] the array,
i
have the following content in the array:

[username\0password\0info\01234]

How can I extract those string from the byte[]?

Hope someone can give me some advice.

Thanks.

Martin
 
Back
Top