Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Monko · May 16, 2013 at 10:40 PM · binary

Find the binary equivalent of a number

I have set up a function that acts like the first 8 digits of binary. Im trying assign each value in the binary code with its decimal equivalent. Look below too see what im trying to do.

 verlistset[0,0,0,0,0,0,0,0,0]
 verlistset[1,0,0,0,0,0,0,0,1]
 verlistset[0,1,0,0,0,0,0,0,2]
 verlistset[1,1,0,0,0,0,0,0,3]
 verlistset[0,0,1,0,0,0,0,0,4]
 verlistset[1,0,1,0,0,0,0,0,5]
 //...247 lines later...
 verlistset[0,1,1,1,1,1,1,1,254]
 verlistset[1,1,1,1,1,1,1,1,255]

This is very inconvenient, because it would require adding 256 lines of code, which would take forever to type and take up a lot of space. What I'm asking is for help to do this faster. My thoughts so far is to set up a loop like this:

 for(i=0;i<256;i+=1){
 x1= //somthing
 x2= //somthing
 x3= //somthing
 x4= //somthing
 x5= //somthing
 x6= //somthing
 x7= //somthing
 x8= //somthing
 verlistset[x1,x2,x3,x4,x5,x6,x7,x8,i]
 }

But I have no Idea how to find the "somthing" value in the code above. Does anyone know how to help?

Comment
Add comment · Show 1
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 robertbu · May 16, 2013 at 11:02 PM 0
Share

Google "Convert Binary to Decimal C#". You will get lots of hits. Here is one page that has half a dozen different ways.

http://stackoverflow.com/questions/9742777/binary-to-decimal-conversion-formula

If you are using Unityscript, things will be a bit different, but any solution that is using .Net should work.

4 Replies

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

Answer by Monko · May 16, 2013 at 11:37 PM

Thank you GerryM! The link you lead me to required a little changing, but I got it to work. Here is what I did:

 function GetIntBinaryString(n:int)
     {
     var b : int[] = new int[8];
     var pos : int = 7;
     var i : int = 0;
     while (i < 8)
     {
         if ((n & (1 << i)) != 0)
         {
         b[pos] = 1;
         }
         else
         {
         b[pos] = 0;
         }
         pos-=1;
         i+=1;
     }
     verlistset(b[7],b[6],b[5],b[4],b[3],b[2],b[1],b[0],n);
     }

 for (var n=0;n<256;n+=1){
 GetIntBinaryString(n);
 }

This code sets my all of the 8 digits of binary to its decimal equivelant. Now I don't need 256 lines of code. Thanks again!

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 Monko · May 16, 2013 at 11:45 PM 0
Share

By the way, im using javascript.

avatar image
1

Answer by GerryM · May 16, 2013 at 11:00 PM

You can find a C# example to convert to a 32-bit binary string here, which should be easy to convert to your array.

In JavaScript you can do something similar with toString(2).

More on logical shift and bitwise operations at Wikipedia.

Comment
Add comment · 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
1

Answer by Bunny83 · May 16, 2013 at 11:17 PM

Uhm, you know that there are binary operators? Like:

  • or: |

  • and: &

  • xor: ^

What's the actual purpose of your strange array? How would you use it once it's initialized? Also if this isn't just pure pseudo code, what do you actually store in this nine-dimensional array?

Anyways. If you have an arbitrary number, for example "42". If you want to know the binary representation you just have to test for each bit:

 ((42 &    1) != 0) = false
 ((42 &    2) != 0) = true
 ((42 &    4) != 0) = false
 ((42 &    8) != 0) = true
 ((42 &   16) != 0) = false
 ((42 &   32) != 0) = true
 ((42 &   64) != 0) = false
 ((42 &  128) != 0) = false

This is the same with a left shift operator:

 (42 & (1<<0)) != 0
 (42 & (1<<1)) != 0
 (42 & (1<<2)) != 0
 (42 & (1<<3)) != 0
 (42 & (1<<4)) != 0
 (42 & (1<<5)) != 0
 (42 & (1<<6)) != 0
 (42 & (1<<7)) != 0

This simple function converts an arbitrary integer into a binary string:

 // C#
 
 string ToBinary(int aVal, int aDigits)
 {
     string result = "";
     for(int i = 0; i < aDigits; i++)
     {
         if (aVal & (1 << i) != 0)
             result = "1" + result;
         else
             result = "0" + result;
     }
     return result;
 }

Examples:

 Debug.Log("56 == " + ToBinary(56,8));       // "56 == 00111000"
 Debug.Log("56 == " + ToBinary(56,16));      // "56 == 0000000000111000"
Comment
Add comment · 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
0

Answer by whydoidoit · May 16, 2013 at 11:12 PM

If you are using that array from your other question you can do:

   for(var i = 0; i < 256; i++) eightDimensionalArray[i] = i;

I am beginning to thing that you should be using bits though, you could do that easily for marching cubes like this:

   const upperTopLeftCorner = 1 << 0;
   const upperTopRightCorner = 1 << 1;
   const upperBottomLeftCorner = 1 << 2;
   const upperBottomRightCorner = 1 << 3;
   const lowerTopLeftCorner = 1 << 4;
   const lowerTopRightCorner = 1 << 5;
   const lowerBottomLeftCorner = 1 << 6;
   const lowerBottomRightCorner = 1 << 7;

Then to work out which of your cubes to use.

  • Set an int value to 0

  • Test each point to see if it is occupied

  • If it is then or the value with the current corner

  • Your final value is you lookup into your predefined cubes!

       var template = 0;
         if(UpperTopLeftCornerIsOccupied()) template |= upperTopLeftCorner;
         if(UpperTopRightCornerIsOccupied()) template |= upperTopRightCorner;
         ...
    
    

At the end of that you will have the number between 0 and 255 which is the template shape you should use.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Can i load my binary with Resources.Load()? 1 Answer

Storing currency, one-time events etc. 1 Answer

Is there a way how to wait for debugger to connect/start and debug built application from VS? 0 Answers

Write/Read binary file 1 Answer

Writing binary files on Android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges