Creating Buffer

  • Thread starter Thread starter BMax
  • Start date Start date
B

BMax

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?
 
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.
 
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 said:
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 said:
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?
 
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 said:
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 said:
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 said:
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?
 
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 said:
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 said:
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 said:
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
Hello
I want to create a byte[] and use it as a buffer, then access it
with
 
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 said:
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 said:
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 said:
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?


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
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?
 
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 said:
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 said:
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 said:
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
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?


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
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?
 
Back
Top