473,487 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

911: byte [] manipulation

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

Nov 17 '05 #1
4 4851
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" <Ma****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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

Nov 17 '05 #2
Hi,

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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Martin" <Ma****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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

Nov 17 '05 #3
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 [MCAD.NET]" wrote:
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" <Ma****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
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


Nov 17 '05 #4
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" <Ma****@discussions.microsoft.com> wrote in message
news:75**********************************@microsof t.com...
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 [MCAD.NET]" wrote:
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" <Ma****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
> 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
>


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
29014
by: xixi | last post by:
i am using db2 udb v8.1 on windows 64 bit i am issue below query by doing sqlStr = INSERT INTO NJIPD.OELTOHY8_OEFTOH VALUES (xx,xxx,xxx, default) insertStatement = conn.createStatement();...
3
4278
by: Prince Kumar | last post by:
When running LOAD with "ALLOW READ ACCESS", I get the following error if select is running againt the table (isolation UR). load.sql --------- db2 load from /u02/data/dly_d040817_test.dat of...
47
16455
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
3
2107
by: Dennis | last post by:
If I have a byte how would I port code from C++ to manipulate the byte array as if it were a long? For example: // C++ // assume the following // unsigned char* m_pPalette; // unsigned...
6
2726
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
4
2437
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have an RGB color held in a Int32 variable. I using an unsafe code to loop through Bitmap data, and by using the BitmapData.Scan0 pointer I need to copy the Int32 color value into the Bitmap...
2
1431
by: lemnitzer | last post by:
TV report: all 15 British sailors confess to illegally entering Iranian Waters Here are the links: http://news.google.com/news?hl=en&ned=us&q=all+15&btnG=Search+News...
24
2264
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
1
2310
by: Rahul Babbar | last post by:
Hi, I am getting a error sqlcode : -911, sqlstate : 40001 error, i.e. Deadlock error while trying to drop the procedure. I had seen cases of getting deadlocks in a table but not in...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
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
5439
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 project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
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...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.