C#: Convert Byte array into a float - c#

I have a byte array of size 4
byte[] source = new byte[4];
Now I wanted to convert this source into a 4-byte float value...
Can anyone tell me how to do this...

Try
float myFloat = System.BitConverter.ToSingle(mybyteArray, startIndex);

Related

String with varbinary to ImageView

I'm trying to send a string contained a varbinary into an ImageView
string ImageHexAsString = "0xFFD8FFE000104A464946000101010...";//Here is my string as VarBinary
byte[] toBytes = Encoding.ASCII.GetBytes(ImageHexAsString); //Here i'm converting string to byte[]
Bitmap bitmap = BitmapFactory.DecodeByteArray(toBytes, 0, toBytes.Length);
imageView.SetImageBitmap(bitmap); //and here i'm send it to imageview
I get an empty white image nothing more.Is something wrong?
UPDATE: Just realized this is about c#, not Java. Answer is the same though. Possible duplicate of How do you convert a byte array to a hexadecimal string, and vice versa?
This is probably a duplicate of Convert a string representation of a hex dump to a byte array using Java?.
What's happening is that you shouldn't try and interpret the hex string as text (with Encoding.ASCII.GetBytes()), because it isn't, it's an image.
SOLVED:
ImageHexAsString = "0xFFD8FFE000104A4649460001010100....";
List<byte> byteList = new List<byte>();
string hexPart = ImageHexAsString.Substring(2);
for (int i = 0; i < hexPart.Length / 2; i++)
{
string hexNumber = hexPart.Substring(i * 2, 2);
byteList.Add((byte)Convert.ToInt32(hexNumber, 16));
}
byte[] original = byteList.ToArray();
Bitmap bitmap = BitmapFactory.DecodeByteArray(original, 0, original.Length);
imageView.SetImageBitmap(bitmap);
it seems to work with 256x256 resolution I dont know how to change the struck if image is bigger

How do i create byte array that contains 64 bits array and how do i convert those bits into hex value?

I want to create byte array that contains 64 bits, How can i get particular bits values say 17th bit, and also how can i get hex value of that index of byte? I did like this, Is this correct?
byte[] _byte = new byte[8];
var bit17=((((_byte[2]>>1)& 0x01);
string hex=BitConverter.ToString(_byte,2,4).Replace("-", string.Empty)
You could use a BitArray:
var bits = new BitArray(64);
bool bit17 = bits[17];
I'm not sure what you mean by the "hex value of that bit" - it will be 0 or 1, because it's a bit.
If you have the index of a bit in a byte (between 0 and 7 inclusive) then you can convert that to a hex string as follows:
int bitNumber = 7; // For example.
byte value = (byte)(1 << bitNumber);
string hex = value.ToString("x");
Console.WriteLine(hex);
You can just use ToString() method.
byte[] arr= new byte[8];
int index = 0;
string hexValue = arr[index].ToString("X");

Convert one byte to signed byte or int

oxfc is negative but I have it as a byte and its value is 252, is there any way to convert it to signed byte or int?
I found this method:
(BitConverter.ToInt16(new byte[2] { 0, 0xfc }, 0) / 256).ToString()
But is there any better way?
You can simply cast it:
byte b = 0xfc;
sbyte s = unchecked((sbyte)b);

"Parameter not valid" error when trying to save the image converted from byte

currently I am testing a script that tries to save an image file converted from a string of HEX, however, when I try to execute the Save command, parameter not valid appears.
// Some junk hex image data
string hexImgData = #"FFD8FFE000104A46494600010200006400640000FFFFD9";
// Call function to Convert the hex data to byte array
byte[] newByte = ToByteArray(hexImgData);
MemoryStream memStream = new MemoryStream(newByte);
// Save the memorystream to file
Bitmap.FromStream(memStream).Save("C:\\img.jpg");
// Function converts hex data into byte array
public static byte[] ToByteArray(String HexString)
{
int NumberChars = HexString.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
}
return bytes;
}
Currently I am still in the process of looking what causes this, please advice.
As was mentioned in the comments, your bitmap format is wrong, all you have is some random hex data and the Bitmap.FromStream method has no idea what to do with it. If you look at this link which discusses how to create a bitmap file with a hex editor it discusses the BitmapHeader, BitmapInfoHeader, and the Pixel RGB Data. I was able to create a bitmap using your code by taking the data from their example and using it.
string bitmapHeader = "424D860000000000000036000000";
string bitmapInfoHeader = "280000000500000005000000010018000000000050000000C40E0000C40E00000000000000000000";
string pixelData = "0000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF000000FF0000FF0000FF0000FF0000FF00";
string hexImgData = bitmapHeader + bitmapInfoHeader + pixelData;
// Call function to Convert the hex data to byte array
byte[] newByte = ToByteArray(hexImgData);
MemoryStream memStream = new MemoryStream(newByte);
pictureBox1.Image = Bitmap.FromStream(memStream);
It seems you need convert incoming string from Base64 to byte array like this:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);

hex to float conversion

I have a 4 byte hex number:
08fdc941
it should be convrted to a float number: 25.25, but I don't know how? I use C#
what is the correct way of converting from hex to float?
From this page on MSDN "How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide)".
string hexString = "43480170";
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
Console.WriteLine("float convert = {0}", f);
// Output: 200.0056
Something like this:
byte[] bytes = BitConverter.GetBytes(0x08fdc941);
if (BitConverter.IsLittleEndian)
{
bytes = bytes.Reverse().ToArray();
}
float myFloat = BitConverter.ToSingle(bytes, 0);
This yields 25.24855, which is what I think you were looking for.
var bytes = BitConverter.GetBytes(0x08fdc941);
Array.Reverse(bytes);
var result = BitConverter.ToSingle(bytes, 0);
Are you sure it's the right way around, since BitConverter.ToSingle(BitConverter.GetBytes(0x08fdc941).Reverse().ToArray(), 0) is close.
Edit:
Incidentally, http://en.wikipedia.org/wiki/Single_precision_floating-point_format gives a pretty good summary of how ISO/IEC/IEEE 60559 (IEEE 754) single-precision floating-point numbers work.
string hexString = 08fdc941;
Int32 IntRep = Int32.Parse(hexString, NumberStyles.AllowHexSpecifier);
// Integer to Byte[] and presenting it for float conversion
float myFloat = BitConverter.ToSingle(BitConverter.GetBytes(IntRep), 0);
// There you go
return myFloat;
For more information, see this:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types

Categories

Resources