-1

I have a binary file with vertex normals. The value is coded as byte. How do I encode angle from byte and convert the angle to float? Example:

binary: 128, 128, 255

obviously in angles it's: 90*, 90*, 180*

How do I get a value in float?

Values are obviously 0.5 0.5 1.0 but what's the c# code so I can convert byte from 128 to 0.5f?

Mr Jox
  • 75
  • 9

1 Answers1

2

byte has a range of [0..255], so if you're mapping it to a [0.0..1.0] range, it's simple math:

double angle = byteValue / 255.0;

At that point, you can then multiply by whatever angle system you desire.

// Degrees
double angleDeg = angle * 360.0;

// Radians
double angleRad = angle * 2.0 * Math.PI;
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • 1
    @MrJox Wrong how? What result are you expecting, and what are you getting? – Abion47 Sep 26 '16 at 07:21
  • another example: binary: 29, 208, 112 float: -0.6084800, 0.7681854, 0.1991063 – Mr Jox Sep 26 '16 at 07:27
  • Then you implemented it wrong. If you manually calculate it, the values are correct. – jAC Sep 26 '16 at 07:29
  • @JanesAbouChleih the result in above example is a correct one and my problem is I can't understand the algorithm of getting these values. It can be anything.. – Mr Jox Sep 26 '16 at 07:33
  • I don't get it? First you say that the result is wrong. Now the result is correct. Do you want an explanation of this basic mathematical construct? Rule of three, there you go. 255 = 360°, so you map your byte value to a range of 0.0 to 1.0 (byte/255), after that you multiply your range to a degree (°) value by multiplying with 360 (full circle). If you need further explanation: math.stackexchange.com or a math book or secondary school. – jAC Sep 26 '16 at 07:39
  • @JanesAbouChleih that's probably the result of the way how I explained the issue, sorry. What I say is this **binary: 29, 208, 112 float: -0.6084800, 0.7681854, 0.1991063** is the correct conversion and I can't understand the algorithm how to do this. Im doing a conversion of vertex normals from byte value to float. I use 3d max to export both binary file and xml file and I compare values. All my attempts so far to convert from binary byte to float didn't result me same values as I have in xml. – Mr Jox Sep 26 '16 at 07:48
  • You should probably update your question with what you really want to accomplish. – jAC Sep 26 '16 at 07:55