Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 141695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:49:04+00:00 2026-05-11T07:49:04+00:00

I use an extension method to convert float arrays into byte arrays: public static

  • 0

I use an extension method to convert float arrays into byte arrays:

public static unsafe byte[] ToByteArray(this float[] floatArray, int count) {     int arrayLength = floatArray.Length > count ? count : floatArray.Length;     byte[] byteArray = new byte[4 * arrayLength];     fixed (float* floatPointer = floatArray)     {         fixed (byte* bytePointer = byteArray)         {             float* read = floatPointer;             float* write = (float*)bytePointer;             for (int i = 0; i < arrayLength; i++)             {                 *write++ = *read++;             }         }     }     return byteArray; } 

I understand that an array is a pointer to memory associated with information on the type and number of elements. Also, it seems to me that there is no way of doing a conversion from and to a byte array without copying the data as above.

Have I understood this? Would it even be impossible to write IL to create an array from a pointer, type and length without copying data?

EDIT: Thanks for the answers, I learned some fundamentals and got to try out new tricks!

After initially accepting Davy Landman’s answer I found out that while his brilliant StructLayout hack does convert byte arrays into float arrays, it does not work the other way around. To demonstrate:

[StructLayout(LayoutKind.Explicit)] struct UnionArray {     [FieldOffset(0)]     public Byte[] Bytes;      [FieldOffset(0)]     public float[] Floats; }  static void Main(string[] args) {     // From bytes to floats - works     byte[] bytes = { 0, 1, 2, 4, 8, 16, 32, 64 };     UnionArray arry = new UnionArray { Bytes = bytes };     for (int i = 0; i < arry.Bytes.Length / 4; i++)         Console.WriteLine(arry.Floats[i]);      // From floats to bytes - index out of range     float[] floats = { 0.1f, 0.2f, 0.3f };     arry = new UnionArray { Floats = floats };     for (int i = 0; i < arry.Floats.Length * 4; i++)         Console.WriteLine(arry.Bytes[i]); } 

It seems that the CLR sees both arrays as having the same length. If the struct is created from float data, the byte array’s length is just too short.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:49:05+00:00Added an answer on May 11, 2026 at 7:49 am

    Yes, the type information and data is in the same memory block, so that is impossible unless you overwrite the type information in a float array to fool the system that it’s byte array. That would be a really ugly hack, and could easily blow up…

    Here’s how you can convert the floats without unsafe code if you like:

    public static byte[] ToByteArray(this float[] floatArray) {     int len = floatArray.Length * 4;     byte[] byteArray = new byte[len];     int pos = 0;     foreach (float f in floatArray) {         byte[] data = BitConverter.GetBytes(f);         Array.Copy(data, 0, byteArray, pos, 4);         pos += 4;     }     return byteArray; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer We have just gone down the route of Windows Server… May 11, 2026 at 9:49 am
  • added an answer This is just due to you doing it from the… May 11, 2026 at 9:49 am
  • added an answer You should look into the magic constants. echo __LINE__; //… May 11, 2026 at 9:49 am

Related Questions

I'm attempting to use extension methods to add an operater overload to the C#
I use an XML file in App_Data in conjunction with a Repeater on the
I use an sql server regularly and have recently been getting frustrated by the
I use an SQL statement to remove records that exist on another database but
I use an external diff tool with Subversion (Beyond Compare rules!), and one of
If I use an URL like http://mysite/myfolder/myfile.dll , I get a dialog Do you
When I use an import such as <script type=text/javascript src=http://o.aolcdn.com/dojo/1.2.3/dojo/dojo.xd.js djConfig=parseOnLoad:true, isDebug: true></script> I
When I use an Iterator of Object I use a while loop (as written
Is it recommended that I use an initialization vector to encrypt/decrypt my data? Will
When should I use an interface and when should I use a base class?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.