By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,391 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,391 IT Pros & Developers. It's quick & easy.

Creating Buffer

P: n/a
Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that, like
readint16, readbytes(),readint32.... . so if there is a method to read byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You may instantiate a System.IO.BinaryReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConverter for conversions from byte sequences into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that, like readint16, readbytes(),readint32.... . so if there is a method to read byte arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?

Nov 16 '05 #2

P: n/a
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_O******************@news4.e.nsc.no...
You may instantiate a System.IO.BinaryReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConverter for conversions from byte sequences into primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a
binaryreader or streamreader.
what is important is to be able to read different variables form that,

like
readint16, readbytes(),readint32.... . so if there is a method to read

byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?


Nov 16 '05 #3

P: n/a
I suppose you mean something like:
int i = (int) GetPrimitive(myBuffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
....
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_O******************@news4.e.nsc.no...
You may instantiate a System.IO.BinaryReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConverter for conversions from byte sequences

into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
Hello
I want to create a byte[] and use it as a buffer, then access it with a binaryreader or streamreader.
what is important is to be able to read different variables form that,

like
readint16, readbytes(),readint32.... . so if there is a method to read

byte
arrays that would also help.
(i couldn' use convert class )
is that possible? if yes how?



Nov 16 '05 #4

P: n/a
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buffer buff , short i) //returns a short
myfunction(buffer buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byte[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:2W******************@news2.e.nsc.no...
I suppose you mean something like:
int i = (int) GetPrimitive(myBuffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
...
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_O******************@news4.e.nsc.no...
You may instantiate a System.IO.BinaryReader/Writer by passing a
MemoryStream
(which in turn is instantiated with the buffer you want to work with)
to the constructor as the base stream.

Have a look at System.BitConverter for conversions from byte sequences

into
primitive data types.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl...
> Hello
> I want to create a byte[] and use it as a buffer, then access it with a
> binaryreader or streamreader.
> what is important is to be able to read different variables form

that, like
> readint16, readbytes(),readint32.... . so if there is a method to read byte
> arrays that would also help.
> (i couldn' use convert class )
> is that possible? if yes how?
>
>



Nov 16 '05 #5

P: n/a
Ok, now I understand;
you have to do it that way because C# does not allow you
to have overloaded methods with the same parameters that only differ in
return type.
So the last parameters of these functions are really just dummy parameters
to get around that
overloading limitation.

My advice on this one is to use output parameters.
That way you can use the same function names, and you may gain some
efficiency
and clearness by not having to send a dummy parameter to indicate the return
type.
You define an output parameter by the keyword "out".

This is how you do it:

private void getPrimitive ( byte [] sourceBuffer, int offset, out int
result )
{
result = System.BitConverter.ToInt32(sourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out float
result )
{
result = System.BitConverter.ToSingle(sourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out string
result )
{
result = System.BitConverter.ToString(sourceBuffer, offset);
}

Now you can call this functions with only necessary parameters:

int resultInt32; // Initialization not needed as it will be passed as out
parameter.
float resultSingle; // Initialization not needed as it will be passed as out
parameter.
string resultString; // Initialization not needed as it will be passed as
out parameter.
byte [] buff = LoadBuffer(); // Should contain your working byte buffer.

// Get a string from the buffer at position 2.
// We must use the out keyword even in the calling context.
getPrimitive(buff, 2, out resultString);

// Get an Int32 from the buffer at position 8:
// We must use the out keyword even in the calling context.
getPrimitive(buff, 8, out resultInt32);

// Get a Single from the buffer at position 82:
// We must use the out keyword even in the calling context.
getPrimitive(buff, 82, out resultSingle);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Bmax" <BluemanREMOVETHISANDPUTATSIGHNgmail.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buffer buff , short i) //returns a short
myfunction(buffer buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byte[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:2W******************@news2.e.nsc.no...
I suppose you mean something like:
int i = (int) GetPrimitive(myBuffer, typeof(int));
object GetPrimitive ( byte sourceBuffer, System.Type returnType )
{
...
}

You could do it like that, but i think you will gain performance
and efficiency by avoiding the type casts that you must then do.

Did i understand your question correct?
If not, let me know, and i will try once again to help you.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"BMax" <sd@asd.c> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks.
didn't use MemoryStream , BitConvertor solved all problems.
But for a better code, do you know how to creat template prototype for
classes? in c# I mean. then i can pass the buffer to a function with

desired
data type and it returns a TypeOf object?
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_O******************@news4.e.nsc.no...
> You may instantiate a System.IO.BinaryReader/Writer by passing a
> MemoryStream
> (which in turn is instantiated with the buffer you want to work with) > to the constructor as the base stream.
>
> Have a look at System.BitConverter for conversions from byte sequences into
> primitive data types.
>
>
> --
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "BMax" <sd@asd.c> wrote in message
> news:uw**************@TK2MSFTNGP10.phx.gbl...
> > Hello
> > I want to create a byte[] and use it as a buffer, then access it with
a
> > binaryreader or streamreader.
> > what is important is to be able to read different variables form

that, > like
> > readint16, readbytes(),readint32.... . so if there is a method to read > byte
> > arrays that would also help.
> > (i couldn' use convert class )
> > is that possible? if yes how?
> >
> >
>
>



Nov 16 '05 #6

P: n/a
Bingo!
I didn't know anything about this Output parameter.now I can optimize my
code.
don't even know how to thank you :)
Tak So mycket.

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:Kz******************@news2.e.nsc.no...
Ok, now I understand;
you have to do it that way because C# does not allow you
to have overloaded methods with the same parameters that only differ in
return type.
So the last parameters of these functions are really just dummy parameters
to get around that
overloading limitation.

My advice on this one is to use output parameters.
That way you can use the same function names, and you may gain some
efficiency
and clearness by not having to send a dummy parameter to indicate the
return
type.
You define an output parameter by the keyword "out".

This is how you do it:

private void getPrimitive ( byte [] sourceBuffer, int offset, out int
result )
{
result = System.BitConverter.ToInt32(sourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out float
result )
{
result = System.BitConverter.ToSingle(sourceBuffer, offset);
}

private void getPrimitive ( byte [] sourceBuffer, int offset, out string
result )
{
result = System.BitConverter.ToString(sourceBuffer, offset);
}

Now you can call this functions with only necessary parameters:

int resultInt32; // Initialization not needed as it will be passed as out
parameter.
float resultSingle; // Initialization not needed as it will be passed as
out
parameter.
string resultString; // Initialization not needed as it will be passed as
out parameter.
byte [] buff = LoadBuffer(); // Should contain your working byte buffer.

// Get a string from the buffer at position 2.
// We must use the out keyword even in the calling context.
getPrimitive(buff, 2, out resultString);

// Get an Int32 from the buffer at position 8:
// We must use the out keyword even in the calling context.
getPrimitive(buff, 8, out resultInt32);

// Get a Single from the buffer at position 82:
// We must use the out keyword even in the calling context.
getPrimitive(buff, 82, out resultSingle);
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Bmax" <BluemanREMOVETHISANDPUTATSIGHNgmail.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
Not really.
what I asked was a function that can return deferent types depends on
parameters.
like :
myfunction(buffer buff , short i) //returns a short
myfunction(buffer buff , myobject p)//returns myobject
for this I defined different version of a same function within a class:

private int func(buffer, int i){}
private byte[] func(buffer,byte[]) {}
private sting ....

so in main class i just pass buffer and desired type to function and it
returns the same type.
but can i avoid writing different version of the same function? here the
template thing can help. then i don't need to handle all types. your code
will work, but is there another way without type cast?
Thank you very much for your helps, it really helps.

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:2W******************@news2.e.nsc.no...
> I suppose you mean something like:
> int i = (int) GetPrimitive(myBuffer, typeof(int));
> object GetPrimitive ( byte sourceBuffer, System.Type returnType )
> {
> ...
> }
>
> You could do it like that, but i think you will gain performance
> and efficiency by avoiding the type casts that you must then do.
>
> Did i understand your question correct?
> If not, let me know, and i will try once again to help you.
>
>
> --
> Regards,
> Dennis JD Myrén
> Oslo Kodebureau
> "BMax" <sd@asd.c> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > Thanks.
> > didn't use MemoryStream , BitConvertor solved all problems.
> > But for a better code, do you know how to creat template prototype
> > for
> > classes? in c# I mean. then i can pass the buffer to a function with
> desired
> > data type and it returns a TypeOf object?
> >
> >
> > "Dennis Myrén" <de****@oslokb.no> wrote in message
> > news:_O******************@news4.e.nsc.no...
> > > You may instantiate a System.IO.BinaryReader/Writer by passing a
> > > MemoryStream
> > > (which in turn is instantiated with the buffer you want to work with) > > > to the constructor as the base stream.
> > >
> > > Have a look at System.BitConverter for conversions from byte sequences > > into
> > > primitive data types.
> > >
> > >
> > > --
> > > Regards,
> > > Dennis JD Myrén
> > > Oslo Kodebureau
> > > "BMax" <sd@asd.c> wrote in message
> > > news:uw**************@TK2MSFTNGP10.phx.gbl...
> > > > Hello
> > > > I want to create a byte[] and use it as a buffer, then access it

with
> a
> > > > binaryreader or streamreader.
> > > > what is important is to be able to read different variables form

that,
> > > like
> > > > readint16, readbytes(),readint32.... . so if there is a method to

read
> > > byte
> > > > arrays that would also help.
> > > > (i couldn' use convert class )
> > > > is that possible? if yes how?
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.