Hello Peter,
Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.
Firstly, I want to confirm that I understand your question correctly. We
are trying to get a System.Single type value by combining four unsigned
char's bit value. If my understanding is incorrect, please feel free to let
me know.
I have found two solutions for this objective in C#:
1.Use the Windows API RtlMoveMemory as you did in VB6. But in this
approach, we need to put our codes in unsafe block to use the pointer. My
codes look like:
class Program
{
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError =
false)]
unsafe static extern void CopyMemory(void* dest, void* src, int size);
static void Main(string[] args)
{
unsafe
{
byte[] byteArray = new byte[] { 107, 62, 139, 65 };
float singleValue = 0f;
float* des = &singleValue;
fixed (byte* src = byteArray)
{
CopyMemory(des, src, 4);
}
Console.WriteLine(singleValue.ToString());
}
}
}
2.Actually, it is not necessary to move memory in this case. Thus a better
way is using the BitConverter class in .NET Framework to convert a byte
array to a single type value. Codes are as follows:
class Program
{
static void Main(string[] args)
{
byte[] s = new byte[] { 107, 62, 139, 65 };
Single test = BitConverter.ToSingle(s, 0);
Console.WriteLine(test.ToString());
}
}
The above two code snippets both give me the value 17.40548. I hope this
will help. Please let me know if you have any future questions or concerns.
I will do my best to help on this.
Best regards,
Ji Zhou (v-****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Best regards,
Ji Zhou