type of variable in Visual C# - c#

I am using DirectSound to record voice and save in binary code.
var problem = _dwCapBuffer.Read(offset, typeof(byte), LockFlag.None, _dwOutputBufferSize);
#"problem" is byte array arrcording to http://msdn.microsoft.com/en-us/library/windows/desktop/bb280844.aspx
but when I use "problem" in function like private string ByteArray_to_Hex(byte[] data)
ByteArray_to_Hex(problem), it show the error
the best overload method....has some valid argument
How can I solve this problem ( use the return value of CaptureBuffer.Read as byte[] ) ?

According to the documentation, it's not returning a byte array but an Array.
Just type ByteArray_to_Hex(problem) and let Visual Studio generate the method. Then you'll see what type it returns. Perhaps you can call ByteArray_to_Hex((byte[])problem) to explicitly cast it.

Related

Why does ITypeSymbol.ToString() return a wrong value for a multidimensional array in Roslyn?

My aim is to get TypeSyntax by ITypeSymbol with Roslyn.
I do it this way:
SF.ParseTypeName(myTypeSymbol.ToString())
This approach works fine until we get myTypeSymbol of multidimensional array in input.
Example:
Say myExpression is of type int[,]
in this case
SemanticModel.GetTypeInfo(myExpression).Type.ToString();
returns int[*,*] instead of int[,].
So SF.ParseTypeName("int[*,*]") returns wrong TypeSyntax.
In the result TypeSyntax rank.sizes are parsed as PrefixUnaryExpressionSyntax instead of OmittedArraySizeExpressionSyntax.
So the questions are why does myTypeSymbol.ToString() returns int[*,*]? and is there any workaround to get correct TypeSyntax for multidimensional array?
If you want to control the string representation of a symbol (to be programming language specific, for example), you need to use a SymbolDisplayFormat and feed it to 'symbol.ToDisplayString()`.

Unable to cast object of type 'Microsoft.SqlServer.Dts.Pipeline.BlobColumn' to type 'System.Byte[]'

I have a script that takes a file (usually image or pdf) from an SQL array, and if necessary decompresses it, and then copies its content to another cell. However, for reasons that I do not understand, the program fails for some uncompressed files with the error: Unable to cast object of type 'Microsoft.SqlServer.Dts.Pipeline.BlobColumn' to type 'System.Byte[]'..
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
if (Row.bitCompressed)
{
byte[] inputData = Row.imgFileContent.GetBlobData(0, (int)Row.imgFileContent.Length);
Row.FileImageOut.AddBlobData(Decompress(inputData));
}
else
{
Row.FileImageOut = Row.imgFileContent; <--- ERROR
}
}
The Row.FilmeImageOur is a Microsoft.SqlServer.Dts.Pipeline.BlobColumn Input0Buffer.FileImageOut while Row.imgFileContent is a Microsoft.SqlServer.Dts.Pipeline.BlobColumn Input0Buffer.imgFileContent .
When searchin for the solution I found this but I don't really understand this guys solution and If it is even applicable. I tried using addBlobData instead of =, but it requires byte input instead of blob data. I can't seem to find any functions which allow us to assign blob data to blob data?
Changed my else code to this, seems to work, though it also seems quite inefficient. First I get blob data to a byte array, then I put that array into add blob data. The fact that I can't simply assign those values sometimes (as it breaks only sometimes) seems like a bug to me
else
{
byte[] copyData = Row.imgFileContent.GetBlobData(0, checked((Int32)Row.imgFileContent.Length));
Row.FileImageOut.AddBlobData(copyData, checked((Int32)Row.imgFileContent.Length));
}

MemoryStream to sbyte[]

I'm still trying to add a jpeg image to a person in Open LDAP, using Novell's c# libraries:
Openldap: Add jpegPhoto to inetOrgPerson
There is a constructor for Novell.Directory.Ldap.LdapAttribute the with the following signature:
public LdapAttribute(string attrName, sbyte[] attrBytes)
So, I plan to use this one. But, how to convert a jpeg image from a MemoreyStream to sbyte[]?
MemoryStream.ToArray()
method returns byte[] and I don't know how to do it.
You can convert the array like this:
Array.ConvertAll(bytes, b => (sbyte)b)
On the CLR you can constant-time convert a byte[] to an sbyte[]. See my previous answer on this little-known trick.
(sbyte[])(object)MemoryStream.ToArray(); //compiles and runs

Passing a C# byte array to LuaInterface

I have a byte array in my C# code that I need to pass into a LuaInterface instance. I can use pack() in Lua, pass the resulting string to C# and convert it with System.Text.Encoding.UTF8.GetBytes(), but going the other way doesn't seem to work.
Is there a simple solution? I'm hoping I can avoid assigning the byte array to a global value.
Edit:
I tried a few new things this morning. I tried using LuaInterface.GetFunction(), and everything works until it hits lua_pushstring() in LuaDLL.cpp. At this point the C# string is converted to a char* via Marshal::StringToHGlobalAnsi().ToPointer(). It looks like this function expects a null terminated string, and my string's first byte is 0 so I get an empty string in my lua code.
Finally traced it down to a the call to ::lua_pushstring() in lapi.c. It called strlen() on the char* passed in. Since my first byte of data was 0, it returned 0. There is an alternate call, lua_pushlstring, that accepts the size of the string as an argument. Changing to call this function fixed the issue.
Try encoding your byte array with System.Text.ASCIIEncoding.ASCII.GetString to get a string that can be passed to Lua.

Reading Java UUID (from DB) in C#

I'm trying to map a table, where one of the column is a binary array (which is a Java UUID), to a C# class.
I would like to read the byte array and return a string representation of the UUID. Just wondering what's a good place to start.
If you've already got it as a byte array, then just call new Guid(bytes) to get a Guid; you can call ToString on that to convert it to a string if you particularly need to. (I'd suggest leaving it as a Guid though other than for diagnostics.)

Categories