This question already has answers here:
problem converting 4-bytes array to float in C#
(3 answers)
Closed 9 months ago.
I have a byte array of four bytes which contains the byts of a FLOAT values.
For example
array[0]=0x1F
array[1]=0x05
array[2]=0x01
array[3]=0x42
this should be 0x4201051f, which means 32.255 value.
Do you have any suggestion on how to group the array and save the value in a float data?
Thank you
As suggested by shingo, the linked question provides the answer:
var input = new byte[] { 0x1F, 0x05, 0x01, 0x42 };
Console.WriteLine(BitConverter.ToSingle(input, 0)); // Outputs 32.255
Related
This question already has answers here:
Convert Hex to Double
(4 answers)
Closed 6 years ago.
i have hex data "44 34 00 00" , and i'll Convert to double "720.0" using C#
like this..
anyone can help? thanks..
What you have is the bytes from a Single (aka float), so do this:
double result = BitConverter.ToSingle(BitConverter.GetBytes(0x44340000), 0);
Console.WriteLine(result); // Prints 720
check that
https://msdn.microsoft.com/en-us/library/system.bitconverter.int64bitstodouble
BitConverter example:
double d = BitConverter.Int64BitsToDouble(0xdeadbeef);
This question already has answers here:
byte[] array pattern search
(29 answers)
Closed 9 years ago.
Supposing I have a byte array of size 4096, what's an efficient way to get the start position of a chunk of say... 5 bytes that matches a pattern?
For e.g., I would like to get the start position of the 1st possible match of the byte array
var match = new byte[] { 0x03, 0x04, 0x05, 0x06, 0x07 };
So if the above chunk is found within my byte array, it will return me the position of the 1st byte (0x03)
You can use Linq:
public static int IndexOfArray<T>(T[] source, T[] search)
{
var result = Enumerable.Range(0, source.Length - search.Length)
.Select(i => new
{
Index = i,
Found = source.Skip(i)
.Take(search.Length)
.SequenceEqual(search)
})
.FirstOrDefault(e => e.Found);
return result == null ? -1 : result.Index;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How i can convert BitArray to single int?
How can I read an integer to a BitArray(6) (assuming it can be contained) and how to convert a BitArray(6) to unsigned/signed integer.
byte[] bytes = { 0, 0, 0, 25 };
// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
BitArray FromInt32(Int32 a)
{
byte[] bytes = BitConverter.GetBytes(a);
return new BitArray(bytes);
}
For reverse operaton see this question as mentioned before.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How do you convert Byte Array to Hexadecimal String, and vice versa, in C#?
C# byte[] to hex string
I need to take this:
byte[] data = new byte[] { 1, 2, 3, 4 }
And turn it into something like this:
0x01020304
What is the best way to do this in C#?
StringBuilder sb = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
sb.AppendFormat("{0:x2}", b)
}
return sb.ToString();
For a single value:
String.Format("{0:X2}", value);
Depending on what the array represents you can then do some string concatenating to put all the bits together.
This question already has answers here:
C# XOR on two byte variables will not compile without a cast [duplicate]
(7 answers)
Closed 4 years ago.
string cetvrtadva = textBox76.Text.Substring(12, 2);
byte cetvrtadvaa = byte.Parse(cetvrtadva,
System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] xor = { 0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa };
Why i cant add byte to byte array?
Error: cannot implicitly convert int to byte.
The issue is that you are not putting a byte into the array, but an int.
It is true that cetvrtadvaa is a byte, but all the other numbers (0x09, 0x45, etc.) you use in the xor operation are ints. Therefore, before ^ is actually done, cetvrtadvaa is converted into an int so that both sides of the operation have the same type.
Therefore, you need to explicitly cast the the result back:
byte[] xor = { (byte)(0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa) };