I am newbie in Low level programming. In my project(C#.NET) we are using EDBS protocol(7 bit format) for communication and i have the data in bit format to send like 00101010 so we would please guide me how to send these to port.I know that the serial port class accepts data in binary format but dont know how convert the bit format into byte .
Thanks in advance
prem
Use the BitConverter class to create basic types (like bytes or ints) from bytes.
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx
There is also a class called BitArray that can be used to store bit representations:
Convert from BitArray to Byte
.Net only receives data in byte (the smallest unit), you can use BitConverter to convert various value type to byte of array and vice verse.
There is a class called BitVector32 which lets you encapsulate your bit values into 32 bit number. Sorry about yesterday's answer, I just found this today.
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());
}
What is the safest way to guarantee that the following operation will be performed correctly:
When I read in 4 bytes as a uint32, I will write it out to a text file.
Later I will open this text file, read the number I wrote out previously, and then convert it back into the 4 bytes for use in other processing.
There is the BitConverter class to help you convert between primitive types and bytes.
Since you are storing this as a string, there isn't a whole lot to this. Obviously there is no issue converting the number into a string using .ToString(). So the only question I assume is how to go back in a reliable fashion. The solution is to use uint.Parse. i.e.:
var s = "12343632423432";
uint i = uint.Parse(s);
(PS: BitConverter is not helpful for conversion from strings)
What would be the best way to continuously send the mouse position to another computer in C#?
So far I use a TCP socket for transportation. I get the ToString() of MousePosition, convert it to byte[] and send that byte array. On the receiving side I append the bytes to a StringBuilder, parse it and construct a new point. At least it works :-)
I'm new to .net (though I know some Java) and I think there is a better way.
Can one serialize / deserialize a System.Drawing.Point in some (elegant) way? Can one send this serialized object to a remote machine? And if so, how?
Regards
Mike
[;-)
Don't bother with either serialization or strings: just send down the pair of coordinates as integers. You can use BitConverter to convert integers into bytes - or just use BinaryWriter for writing and BinaryReader for reading.
System.Drawing.Point is already marked with <Serializable>, so if you want to, you can use .NET's built in serialization.
in languages like PHP or Python there are convenient functions to turn an input string into an output string that is the HEXed representation of it.
I find it a very common and useful task (password storing and checking, checksum of file content..), but in .NET, as far as I know, you can only work on byte streams.
A function to do the work is easy to put on (eg http://blog.stevex.net/index.php/c-code-snippet-creating-an-md5-hash-string/), but I'd like to know if I'm missing something, using the wrong pattern or there is simply no such thing in .NET.
Thanks
The method you linked to seems right, a slightly different method is showed on the MSDN C# FAQ
A comment suggests you can use:
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string, "MD5");
Yes you can only work with bytes (as far as I know). But you can turn those bytes easily into their hex representation by looping through them and doing something like:
myByte.ToString("x2");
And you can get the bytes that make up the string using:
System.Text.Encoding.UTF8.GetBytes(myString);
So it could be done in a couple lines.
One problem is with the very concept of "the HEXed representation of [a string]".
A string is a sequence of characters. How those characters are represented as individual bits depends on the encoding. The "native" encoding to .NET is UTF-16, but usually a more compact representation is achieved (while preserving the ability to encode any string) using UTF-8.
You can use Encoding.GetBytes to get the encoded version of a string once you've chosen an appropriate encoding - but the fact that there is that choice to make is the reason that there aren't many APIs which go straight from string to base64/hex or which perform encryption/hashing directly on strings. Any such APIs which do exist will almost certainly be doing the "encode to a byte array, perform appropriate binary operation, decode opaque binary data to hex/base64".
(That makes me wonder whether it wouldn't be worth writing a utility class which could take an encoding, a Func<byte[], byte[]> and an output format such as hex/base64 - that could represent an arbitrary binary operation applied to a string.)