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.)
Related
I have to query an oracle database for various numeric values and dump them with my c# console app in a binary file with a custom format. Depending on the business data I need to encode the numeric value on 1 byte length,2 byte length,3 byte length,4,6,8,10 and 16...
As for now I think that I could store the 1 byte as a char or a byte value type and write with the standard binarywriter. For the 2 byte length I could use a short value type, etc... But I am pretty sure that there is no native .Net numeric type for the 3 byte length, the 10 byte length and so on...
So I am trying to find how to query the values (from oracle as string ?) and binary write them...
The two solutions I have in mind : write a custom binarywriter or try to find how to create some custome numeric type class (something like Byte10,Byte16...) but both solutions seems akwards....
How would you have deal with that problem? Please do not advise to switch to c/c++ as I do not really know those languages...
Thank you for any help.
Yes it is all about managing a custom byte[] and I think I have found what I was looking for here
I have recently started learning C# Networking and I was wondering how would you tell if the received Byte array is a file or a string?
A byte array is just a byte array. It's just got data in.
How you interpret that data is up to you. What's the difference between a text file and a string, for example?
Fundamentally, if your application needs to know how to interpret the data, you've got to put that into the protocol.
A byte array is just a byte array. However, you could make the original byte array include a byte that describes what type it is (assuming you are the originator of it). Then you find this descriptor byte and use it to make decisions.
Strings are encoded byte arrays; files can contain strings and/or binary data.
ASCII strings use byte values between 0-127 to represent characters and control codes. For UTF8 people have written validation routines (https://stackoverflow.com/a/892443/884862).
You'd have to check the array for all of the string encoding characteristics before you could assume it's a binary file.
edit Here's an SO question about classifying a file type Using .NET, how can you find the mime type of a file based on the file signature not the extension using a signature (first X bytes) of the file to determine it's mimetype.
No you can't. Data is data, you must layer on top of your network communication form of protocol, it will need to say something like: "If the first byte I see is a 1 the next four bytes represent a int, if I see a 2 read the next byte and that is the length of the text string that follows that..."
A much easier solution than inventing your own protocol is use a prebuilt one that gives you a higher level abstraction like WCF so you don't need to deal with byte arrays.
Not quite a "file", an array contains data. You should loop through that array and write the data,
Try this:
foreach(string data in array)
{
Console.WriteLine(data);
}
Now, if it doesn't contain strings, but data, you can simply use a
foreach(var data in array)
{
Console.WriteLine(data.ToString());
}
I have a BigInteger serialized to a file by a Java program using the writeObject method from ObjectOutputStream.
Can I deserialize it in C#? I tried using the java.math and java.io classes of vjslib, but I get an exception:
InvalidClassException
the class does not match the class of the persisted object for cl = java.lang.Number : __SUID = -8742448824652078965, getSUID(cl) = 3166984097235214156
Any ideas?
Do you have control over the serialization step from Java?
If so, I would suggest serializing a byte array, either as binary, or base64, and reading the byte array from the serialized structure.
Then you can pass the byte array to the System.Numerics.BigInteger constructor.
If you don't mind ugly hacks: I'd say the easiest (albeit not most efficient) way would be to just write it out as an ASCII String on the Java side, and parse that string on the C# side, instead of using binary de/serialization.
I suggest you don't use serialization for this, since the two versions of BigInteger are not compatible - they have different version ids.
You should write the object out in some other way, probably using the byte array from BigInteger.toByteArray
Reading this this question about serialization might also be insightful for you
I got a flat file where the data is not delimetered or something else.
The file contains one large string and one row is represented by 180 chars.
Each column value is definied by a length of chars.
I have to create an object for each row, parse the 180 chars and fill
properties of the created object with the parsed values.
How can i solve this problem without permanent using substring or something else?
Maybe some nice solution with Linq?
Thanks a lot.
Solution 1 - Super fast but unsafe:
Create your class with [StructLayout(LayoutKind.Sequential)] and all other unmanaged code markings for length. Your strings will be char array but can be exposed as string after loading.
Read 180 bytes and create a byte array of the same size inside a fixed block
Change pointer to IntPtr and use Marshal.PtrToStructure() to load an onject of your class
Solution 2 - Loading logic in the class:
Create a constructor in your class that accepts byte[] and inside the objects using Covenrt.Toxxx or Encoding.ASCII.ToString() assuming it is ASCII
Read 180 bytes and create an object and pass it to .ctor
If you have to serialise back to byte[] then implement a ToByteArray() method and again use Covenrt.Toxxx or Encoding.ASCII.ToString() to write to byte.
Enhancement to solutions 2:
Create custom attributes and decorate your classes with those so that you can have a factory that reads metadata and inflates your objects using byte array for you. This is most useful if you have more than a couple of such classes.
Alternative to solutions 2:
You may pass stream instead of a byte array which is faster. Here you would use BinaryReader and BinaryWriter to read and write values. Strings however is a bit trick since it writes the length as well I think.
Use a StringReader to parse your text, then you won't have to use substring. Linq won't help you here.
I agree with OJ but even with StringReader you will still need the position of each individual value to parse it out of the string...there is nothing wrong with substring just make sure you use static constants when defining the begging and ending lengths. Example:
private static int VAR_START_INDEX = 0;
private static int VAR_END_INDEX = 4;
String data = "thisisthedata";
String var = data.Substring(VAR_START_INDEX,VAR_END_INDEX);
//var would then be equal to 'this'
This library can help you http://f2enum.codeplex.com/
I'm wondering if string.Length in C# is an instant variable. By instant variable I mean, when I create the string:
string A = "";
A = "Som Boh";
Is length being computed now?
OR
Is it computed only after I try to get A.Length?
Firstly, note that strings in .NET are very different to strings stored in unmanaged languages (such as C++)...
In the CLR, the length of the string (in chars and in bytes) is in fact stored in memory so that the CLR knows how large the block of memory (array of chars) containing the string is. This is done upon creation of the string and doesn't get changed given that the System.String type is immutable.
In C++ this is rather different, as the length of a string is discovered by reading up until the first null character.
Because of the way memory usage works in the CLR, you can essentially consider that getting the Length property of a string is just like retrieving an int variable. The performance cost here is going to be absolutely minimal, if that's what you're considering.
If you want to read up more about strings in .NET, try Jon Skeet's article on the topic - it seems to have all the details you might ever want to know about strings in .NET.
The length of the string is not computed, it is known at construction time. Since String is immutable, there will be no need for calculating it later.
A .NET string is stored as a field containing the count of characters, and a corresponding series of unicode characters.
.NET strings are stored with the length pre-computed and stored at the start of the internal structure, so the .Length property simply fetches that value, making it an O(1) function.
It looks like it is a property of string, which is probably set in the constructor. Since it is not a function, I doubt that it is computed when you call it. They are simply getting the value of the Length property.