• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pulsar19 · Dec 16, 2018 at 09:59 PM · intconvertbytearray

ByteArray impossible to convert... ?

alt text

PS : (My post is a picture because I have :''This post is currently awaiting moderation. contact a system administrator.'' everytime ... I guess it is the fault of number.)

tt.png (46.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Dec 16, 2018 at 11:39 PM

Well, there are several things unclear which you have to clear up first:

  • What exact format are those values in.

  • What endianess does your system use.

To be the values look right if we assume that the first 4 bytes form a 32 bit integer in big endian format and the second value is a IEEE754 single floating point format, also in big endian. You have shown the byte values as decimal numbers but it makes more sense to show them in hexadecimal since it makes it easier to arrange them as a single number


0, 48, 0, 3 is just 00, 30, 00, 03 in hex. Since the bytes are in big endian the first byte is the most significant byte so the whole number is just 0x00300003. This is equivalent to the decimal number "3145731".


Likewise the decimal bytes 63, 83, 91, 53 are in hex 3F, 53, 5B, 35 or written as a single 32 number 0x3F535B35. By quickly using the IEEE754 online converter we can verify that this binary representation does indeed represent a floating point number that is equal to "0.82561046"


To convert this byte array in C# there are several ways to do this. The easiest way is to use the BitConverter class. However you have to adjust the order of the bytes depending on the system you are on. Usually all windows based desktop systems use little endian. Since your values are in big endian you would need to reverse the byte order of the 4 bytes which make up one value. If you are on a Mac or any other system that uses big endian you should be able to use the BitConverter right away.


For example

 // big endian system
 byte[] bytes;
 
 int intVal = System.BitConverter.ToInt32(bytes, 0);
 float fVal = System.BitConverter.ToSingle(bytes, 4);


 // little endian system
 void SwapBytes(bytes[] aBytes, int aIndex)
 {
     var b0 = aBytes[aIndex + 0];
     var b1 = aBytes[aIndex + 1];
     var b2 = aBytes[aIndex + 2];
     var b3 = aBytes[aIndex + 3];
     aBytes[aIndex + 0] = b3;
     aBytes[aIndex + 1] = b2;
     aBytes[aIndex + 2] = b1;
     aBytes[aIndex + 3] = b0;
 }
 // [ ... ]
 
 byte[] bytes;
 
 SwapBytes(bytes, 0);
 int intVal = System.BitConverter.ToInt32(bytes, 0);
 SwapBytes(bytes, 4);
 float fVal = System.BitConverter.ToSingle(bytes, 4);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pulsar19 · Dec 17, 2018 at 01:43 PM 0
Share

Thank you so much !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turning Int To String (50 > "Fifty") 1 Answer

Turn float to int 2 Answers

[SOLVED] Convert this line from string to int 1 Answer

js "Cannot convert 'String' to 'int'." error 1 Answer

Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges