How can i convert this VBNet code into C#? (ByteToImage is a User Defined Function use to convert Byte Array into Bitmap.
Dim Bytes() As Byte = CType(SQLreader("ImageList"), Byte())
picStudent.Image = jwImage.ByteToImage(Bytes)
I tried
byte[] Bytes = Convert.ToByte(SQLreader("ImageList")); // Error Here
picStudent.Image = jwImage.ByteToImage(Bytes);
but it generates an error saying: Cannot implicitly convert type 'byte' to 'byte[]'
What i am doing is basically converting an Image from database to byte array and displaying it on the picturebox.
byte[] Bytes = (byte[]) SQLreader("ImageList");
picStudent.Image = jwImage.ByteToImage(Bytes);
Try this
byte[] Bytes = (byte[])SQLreader("ImageList");
Hope this helps
The problem is you have an array of bytes (byte[] in C# and Byte() in VB.Net) but the Convert.ToByte call just returns a simple byte. To make this work you need to cast the return of SQLreader to byte[].
There is no perfect analogous construct for CType in C# but a simple cast here should do the trick
byte[] Bytes = (byte[])SQLreader("ImageList");
CType is the equivalent of a type cast, not an actual conversion.Besides, Convert.ToByte tries to convert its input to a single byte, not an array. The equivalent code is
byte[] bytes=(byte[])SQLreader("ImageList");
Related
I am writing a function to help serialize data to pass through a socket. I wanted to write a small function that would serialize one item.
private byte[] SerializeOne<T>(T data)
{
byte[] oneItem = new byte[Constants.ONE_ITEM_BUFFER];
oneItem = BitConverter.GetBytes(data);
if (BitConverter.IsLittleEndian)
oneItem.Reverse();
return oneItem; }
The problem is BitConverter alwaysassumes data is a type bool and throws this error: Argument 1 cannot convert from T to bool. Am I missing some syntax to force BitConverter to use the T type or is this not possible in C#?
As per my last question I'm borrowing some code from the Opus project to integrate into VB.NET software.
Consider
byte[] buff = _encoder.Encode(segment, segment.Length, out len);
which I've translated to:
Dim buff(wavEnc.Encode(segment, segment.Length, len)) As Byte
It is throwing a:
Value of type '1-dimensional array of Byte' cannot be converted to 'Integer' error...
How can I fix this problem?
Try this:
Dim buff = wavEnc.Encode(segment, segment.Length, len)
Of course you can do a direct translation of the c#:
Dim buff As Byte() = wavEnc.Encode(segment, segment.Length, len)
No need for a type at all - let the compiler figure it out.
_encoder.Encode() is the right-hand side of an assignment. The left-hand side is a byte array.
The way you are using it in your VB sample is as an array dimensioner: an Integer.
I have some table in Sql Server which has VarBinary(MAX) and I want to upload files to them, I need to make the Dbml to make the field byte[] but instead I get Systm.Data.Linq.Binary.
Why is that and how to make the default as byte[]? Thanks.
I read the file in my MVC 3 coroller action like this (resourceFile is a HttpPostedFileBase and newFile has the byte[])
newFile.FileContent = new byte[resourceFile.ContentLength];
resourceFile.InputStream.Read(newFile.FileContent, 0, resourceFile.ContentLength);
The System.Linq.Data.Binary type is a wrapper for a byte array that makes the wrapped byte array immutable. See msdn page.
Sample code to illustrate immutability:
byte[] goesIn = new byte[] { 0xff };
byte[] comesOut1 = null;
byte[] comesOut2 = null;
byte[] comesOut3 = null;
System.Data.Linq.Binary theBinary = goesIn;
comesOut1 = theBinary.ToArray();
comesOut1[0] = 0xfe;
comesOut2 = theBinary.ToArray();
theBinary = comesOut1;
comesOut3 = theBinary.ToArray();
The immutability can be seen after changing the value of the first byte in comesOut1. The byte[] wrapped in theBinary is not changed. Only after you assign the whole byte[] to theBinary does it change.
Anyway for your purpose you can use the Binary field. To assign a new value to it do as Dennis wrote in his answer. To get the byte array out of it, use the .ToArray() method.
You can use Binary to store byte[] as well, because Binary has implicit conversion from byte[]:
myEntity.MyBinaryProperty = File.ReadAllBytes("foo.txt");
How can I convert sbyte[] to base64 string?
I cannot convert that sbyte[] to a byte[], to keep interoperability with java.
You absolutely can convert the sbyte[] to a byte[] - I can pretty much guarantee you that the Java code will really be treating the byte array as unsigned. (Or to put it another way: base64 is only defined in terms of unsigned bytes...)
Just convert to byte[] and call Convert.ToBase64String. Converting to byte[] is actually really easy - although C# itself doesn't provide a conversion between the two, the CLR is quite happy to perform a reference conversion, so you just need to fool the C# compiler:
sbyte[] x = { -1, 1 };
byte[] y = (byte[]) (object) x;
Console.WriteLine(Convert.ToBase64String(y));
If you want to have a genuine byte[] you can copy:
byte[] y = new byte[x.Length];
Buffer.BlockCopy(x, 0, y, 0, y.Length);
but personally I'd stick with the first form.
class Program
{
static void Main()
{
sbyte[] signedByteArray = { -2, -1, 0, 1, 2 };
byte[] unsignedByteArray = (byte[])(Array)signedByteArray;
Console.WriteLine(Convert.ToBase64String(unsignedByteArray));
}
}
I need do create a new instance of String from the array of sbytes (sbyte[]).
For that I need to convert sbyte[] into sbyte*
It is possible only using unsafe keyword.
is that okay or is there any other ways to create a String from array of sbytes?
First:
How to convert a sbyte[] to byte[] in C#?
sbyte[] signed = { -2, -1, 0, 1, 2 };
byte[] unsigned = (byte[]) (Array)signed;
Then:
string yourstring = UTF8Encoding.UTF8.GetString(unsigned);
Why are you using sbyte?
Encoding.Default.GetString() (and any other encoding) takes a byte[] Array as argument, so you could convert the sbyte[] Array using LINQ if all values are non-negative: array.Cast<byte>().ToArray().