Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Problem:
I am trying to convert a byte[] to a single. I've tried using BitConverter.ToSingle() and it doesn't give the desired result.
The Content of the Array is:
0
0
0
100
The desired output is 100; I know an Int would work for this but I just choose that number for easy debugging. I have also tried moving the 100 into every possible position in the array, with no luck.
My output always looks like 9.3345534545E
or something similar with different digits.
Any Ideas?
IEE-754 types (Single and Double - float and double in C#) do not have a trivial binary representation so 0x00 0x00 0x00 0x64 does not represent the value of 0x64 (100 in decimal).
The actual raw, binary representation of IEEE-754 values is rather complicated and setting them and performing the conversion from integer to IEE-754 really isn't worth the effort (unless it's a learning exercise). It's best to let the library/platform or even the processor do it for you:
Because your value is an integer value, you need to convert it into Int32 first, and then use the Convert class (or a simple compiler cast which will perform the type conversion under-the-hood).
Int32 val = BitConverter.ToInt32( yourArray ); // assuming it's little-endian
Single s1 = (Single)val;
Single s2 = Convert.ToSingle( val );
Related
This question already has answers here:
How do I convert an array of floats to a byte[] and back?
(4 answers)
Closed 1 year ago.
I want to convert 794.328247 value to byte.
I used Convert.ToByte(794.328247) but it showing following error,
Error : - Value was either too large or too small for an unsigned byte.
So anyone can help me to resolve this issue.
Thanks.
You probably want Bitconverter.GetBytes
byte[ ] byteArray = BitConverter.GetBytes( 794.328247);
Note that this produce an array of bytes since a float is 32bit, so it needs 4 bytes to store it. Do the reverse with ToSingle.
The alternative is to truncate the float: var b = (byte) 794.328247;, but this is usually not a good idea since a byte has a far smaller range of values that a float. In some cases you might need to first normalize the input value to some range of values before truncating. I.e. you would remap values from say 750-800 to the 0-255 range.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have the following code:
void MyBitMaker(int inputNumber)
{
Console.WriteLine(inputNumber << 1);
//TODO
//Bit bitHolder = inputNumber << 1;
//InitMico(ref bitHolder);
}
There is not a type for storing bits in C#. Is there any way to store bits in a variable.
I am programming a micro controller using C#, It uses the bits that are coming from a web service, fetches them and sends them to the controller to open, close, sleep and such things using an interface to change the input data to appropriate bits. My problem is that the micro has just 16 bytes memory in ram and I can not store more than two bytes, the micro should store the history of previous actions (this is extra, maybe, jargon). I have low space and need this small unit. I searched a lot and did not find anything. currently I am using bit operators using a class that I myself have implemented but it's not efficient at all because of using bit operations. I was wondering if someone has faced something that can help me.
The smallest addressable unit is a byte, so use that, or a bool which is still 8 bits, but only has two possible values to be set.
You can't reference a bit, so that would be useless anyway. If you need to address a specific position in a byte, you can pass along the position. Still, you can only change the bit's value by setting the entire byte.
I just want to store bits
Trivial solution: bool array.
If it really needed to store the bits in a compacted form, you can use the BitArray type, which uses an int array internally. You can index it similarly to a normal array:
var myBits = new BitArray(20); // initialize for 20 bits (1 int will be stored internally)
myBits[5] = true; // similar to this: myInt |= 1 << 5;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am developing a windows forms application. I have a double value 1.5.That value I need to convert into a two byte array. But when I am converting using BitConverter.GetBytes, getting 8 bytes of data. Please refer my code below.
double d = 1.5;
byte[] array = BitConverter.GetBytes(d);
Double is 8 byte value, so if you have an arbitrary double there's no hope for you; however, if you have some restrictions, e.g. the value is in the [0..100] range and has at most 2 digits after the decimal point, you can encode it:
// presuming source in [0..100] with at most 2 digit after decimal point
double source = 1.5;
// short is 2 byte value
short encoded = (short) (source * 100 + 0.5);
byte[] array = BitConvertor.GetBytes(encoded);
// decode back to double
double decoded = encoded / 100.0;
A double is a 64 bit value that just so happens to be 8 bytes. There's nothing that can be done about this.
A double is 8 bytes in length, so unless you want a specific sub-array out of the 8 you're getting (you probably don't), then this is the wrong way to go about it.
You can cast your variable to a single-precision float. That will of course lose some precision, but you will get 4 bytes instead of 8.
If this is still unacceptable, you need to have an implementation of a half-precision float, which sadly doesn't come out-of-the-box with .NET.
I found an implementation here:
http://sourceforge.net/p/csharp-half/code/HEAD/tree/System.Half
You can use it like this:
var half = (Half)yourDouble;
var bytes = Half.GetBytes(half); // 2 bytes
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So a common question you see on SO is how to convert between type x and type z but I want to know how does the computer do this?
For example, how does it take an int out of a string?
My theory is that a string is a char array at its core so its going index by index and checking it against the ascii table. If it falls within the range of ints then its added to the integer. Does it happen at an even lower level than this? Is there bitmasking taking place? How does this happen?
Disclaimer: not for school, just curious.
This question can only be answered when restricting the types to a somewhat managable subset. To do so, let us consider the three interesting types: Strings, integers and floats.
The only other truly different basic type is a pointer, which is not usually converted in any meaningful manner (even the NULL check is not actually a conversion, but a special built in semantic for the 0 literal).
int to float and vice versa
Converting integers to floats and vice versa is simple, since modern CPUs provide an instruction to deal with that case directly.
string to integer type
Conversion from string to integer is fairly simple, because no numeric errors will happen. Indeed, any string is just a sequence of code points (which may or may not be represented by char or wchar_t), and the common method to work through this goes along the lines of the following:
unsigned result = 0;
for(size_t i = 0; i < str.size(); ++i) {
unsigned c = str[i] - static_cast<unsigned>('0');
if(c > '9') {
if(i) return result; // ok: integer over
else throw "no integer found";
}
if((MAX_SIZE_T - c) / 10 < result) throw "integer overflow";
result = result * 10 + c;
}
If you wish to consider things like additional bases (e.g. strings like 0x123 as a hexadecimal representation) or negative values, it obivously requires a few more tests, but the basic algorithm stays the same.
int to string
As expected, this basically works in reverse: An implementation will always take the remainder of a division by 10 and then divide by 10. Since this will give the number in reverse, one can either print into a buffer from the back or reverse the result again.
string to floating point type
Parsing strings to a double (or float) is significantly more complex, since the conversion is supposed to happen with the highest possible accuracy. The basic idea here is to read the number as a string of digits while only remembering where the dot was and what the exponent is. Then, you would assemble the mantissa from this information (which basically is a 53 bit integer) and the exponent and assemble the actual bit pattern for the resulting number. This would then be copied into your target value.
While this approach works perfectly fine, there are literally dozens of different approaches in use, all varying in performance, correctness and robustness.
Actual implementations
Note that actual implementations may have to do one more important (and horribly ugly) thing, which is locale. For example, in the German locale the "," is the decimal point and not the thousands seperator, so pi is roughly "3,1415926535".
Perl string to double
TCL string to double
David M. Gay AT&T Paper string to double, double to string and source code
Boost Spirit