byte[] <=> sbyte[]

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

Guest

Hi;

Is there a way to cast a byte[] to a sbyte[]? I know I can copy it but if I
have a 32K array, that copy takes time & memory and yet the actual values are
identical in both arrays.

So is there some way to cast a variable in this case?
 
You could implicitly convert it to short[] and then implicitly convert
short[] to sbyte[], and vice versa.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
Tried:

byte[] data = new byte[4];
data[1] = 3;
short[] d2 = (short[])data;

Got the error:

error VJS1333: Cannot cast from 'byte[]' to 'short[]'

--
thanks - dave


Kevin Spencer said:
You could implicitly convert it to short[] and then implicitly convert
short[] to sbyte[], and vice versa.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

David Thielen said:
Hi;

Is there a way to cast a byte[] to a sbyte[]? I know I can copy it but if
I
have a 32K array, that copy takes time & memory and yet the actual values
are
identical in both arrays.

So is there some way to cast a variable in this case?
 
Is there a way to cast a byte[] to a sbyte[]?

No, I think your best option is to make a copy of the array with
Buffer.BlockCopy.


Mattias
 
Hi;

That's what I'm doing. But it grates to copy all that data each time when
it's identical and just needs to be cast. Oh the joys of C++ <g>.

--
thanks - dave


Mattias Sjögren said:
Is there a way to cast a byte[] to a sbyte[]?

No, I think your best option is to make a copy of the array with
Buffer.BlockCopy.


Mattias
 
Oh the joys of C++ <g>.

Well if you like the non type safe hacks you can do in C++ you can
accomplish sort of the same thing in C# unsafe code by pinning the
byte[] to get a byte* and then cast that to a sbyte*. But you still
can't make it a sbyte[].


Mattias
 
Try:

byte[] b1 = new byte[] { 1, 2, 3, 4 };
sbyte[] b2 = new sbyte[4];
for (int i = 0; i < 4; i++)
b2 = (sbyte)b1;

Using explicit casting of the individual elements, this can do it without an
intermediate cast. I tested it on both 1.1 and 2.0 platforms.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

David Thielen said:
Tried:

byte[] data = new byte[4];
data[1] = 3;
short[] d2 = (short[])data;

Got the error:

error VJS1333: Cannot cast from 'byte[]' to 'short[]'

--
thanks - dave


Kevin Spencer said:
You could implicitly convert it to short[] and then implicitly convert
short[] to sbyte[], and vice versa.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

David Thielen said:
Hi;

Is there a way to cast a byte[] to a sbyte[]? I know I can copy it but
if
I
have a 32K array, that copy takes time & memory and yet the actual
values
are
identical in both arrays.

So is there some way to cast a variable in this case?
 
ubyte[] rtn = new ubyte[data.length];
System.Buffer.BlockCopy(data, 0, rtn, 0, rtn.length);
return rtn;

Is even more efficient. But it is still copying all that data...

--
thanks - dave


Kevin Spencer said:
Try:

byte[] b1 = new byte[] { 1, 2, 3, 4 };
sbyte[] b2 = new sbyte[4];
for (int i = 0; i < 4; i++)
b2 = (sbyte)b1;

Using explicit casting of the individual elements, this can do it without an
intermediate cast. I tested it on both 1.1 and 2.0 platforms.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

David Thielen said:
Tried:

byte[] data = new byte[4];
data[1] = 3;
short[] d2 = (short[])data;

Got the error:

error VJS1333: Cannot cast from 'byte[]' to 'short[]'

--
thanks - dave


Kevin Spencer said:
You could implicitly convert it to short[] and then implicitly convert
short[] to sbyte[], and vice versa.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

Hi;

Is there a way to cast a byte[] to a sbyte[]? I know I can copy it but
if
I
have a 32K array, that copy takes time & memory and yet the actual
values
are
identical in both arrays.

So is there some way to cast a variable in this case?
 
So why don't you use byte[] for both arrays in the first place? Is there a
specific reason?
 
Yep, this is a J# project (common java code for java & .net). In java & J#
byte is signed. In .net it's unsigned. So when I have to pass a j# byte[] to
a .net method, I have to convert it to a .net sbyte[].

The actual byte values don't change. That's what makes this frustrating - it
should allow a cast IMHO.

--
thanks - dave


cody said:
So why don't you use byte[] for both arrays in the first place? Is there a
specific reason?


David Thielen said:
Hi;

Is there a way to cast a byte[] to a sbyte[]? I know I can copy it but if
I
have a 32K array, that copy takes time & memory and yet the actual values
are
identical in both arrays.

So is there some way to cast a variable in this case?
 
Back
Top