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 Diego89 · May 15, 2014 at 11:28 AM · arraymathinteger

How to convert and integer into an Array of his digits

Hi guys,

I'm working on a simple game and I have an interger made of X digits let's say 0123456789. now what I would like to do is to obtain an array that has X spaces in which each one contain only one of the digits of the integer. If it easier It will be fine even with a string since than I can convert string into integer.

Thank you.

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

4 Replies

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

Answer by mattyman174 · May 15, 2014 at 12:11 PM

Try this on for size.

 public static int[] IntToIntArray(int num)
     {
         if (num == 0)
             return new int[1] { 0 };
 
         List<int> digits = new List<int>();
 
         for (; num != 0; num /= 10)
             digits.Add(num % 10);
 
         int[] array = digits.ToArray();
         System.Array.Reverse(array);
 
         return array;
     }

Modulo is so wonderful :)

Essentially all your doing here is dividing the original given integer by 10 each time thereby forcing each individual number from the end to beginning into a decimal and using modulo we can just get that number each time.

Then simply reverse the array created and you have the number as an int array.

Just to note some constraints on this solution though, you cant give it negative integers and be careful you dont start trying to feed it something bigger than an integer, like a long.

Comment
Add comment · Show 3 · 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 Diego89 · May 15, 2014 at 12:14 PM 0
Share

thank that is pefect

avatar image petey · Nov 23, 2016 at 09:02 AM 0
Share

This was super handy! Thanks :) $$anonymous$$odulo is cool :D

avatar image Harinezumi · Mar 20, 2018 at 08:55 AM 1
Share

One small detail is that C#'s modulo operator returns a negative number for negative values. If you want the digits without sign, you should first $$anonymous$$athf.Abs() the value before adding it to digits.

avatar image
0

Answer by Naveen Kumar · May 15, 2014 at 11:55 AM

U can Write Like

lets say no = 123456789; int rem = 0; vector arr;

While( no ) {

rem = no%10;

arr.pushback( rem );

no = no/10;

}

And U can have all the digits in reverse order

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 shadabeqbal · Mar 20, 2018 at 07:34 AM

static int[] Int_to_array(int n)

{

int j = 0;

int len = Integer.toString(n).length();

int[] arr = new int[len];

while(n!=0)

{

arr[len-j-1]=n%10;

n=n/10;

j++;

}

return arr;

}

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 Jamal94 · Aug 14, 2020 at 04:01 PM

int[] toArray(int n) { int[] arr = new int[len(n)]; for (int i = arr.length - 1; i >= 0; i--) { arr[i] = n % 10; n = n / 10; } return arr; } int len(int n) { int count = 0; while (n > 0) { n = n / 10; count++; } return count; }

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

27 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

Related Questions

A node in a childnode? 1 Answer

Array of GameObjects - JS 1 Answer

Array iteration problem 1 Answer

Sequential GameObject Activation 2 Answers

C# Children and SubChildren Null Reference Exception 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