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 save · Jan 07, 2012 at 05:16 PM · arrayintsplit

Split each number in an int without converting to char and string first?

I would like to separate each number in an int variable to an array (similar to ToCharArray). Let's say I have the number 123, then I'd like to put that into an array which looks like this: [0,0,0,0,1,2,3] (as the final step).

Is there a way of achieving this without converting it to a string first? Performance is key as it has to run on an iPhone 3GS as minimum.

Doing something like this works, but I'm afraid it might be way to intense (what do you think?):

 private var numbers : int = 1234567; //This can be 0 to 9999999
 private var numArray : int[] = [0,0,0,0,0,0,0]; //The array to store the int numbers 

 function Start () {
     InvokeRepeating("NumToArray", 0, 0.1); //Or some other acceptable repeated value
 }

 function NumToArray () {
     var numString : String = numbers.ToString();
     var numCharArray : char[] = numbers.ToCharArray();
     var pos : int = 6;
     for (var i : int = numCharArray.Length-1; i>=0; i--) {
         numArray[pos] = parseInt(scoreStringArray[i].ToString());
         pos--;
     }
 }
Comment
Add comment · Show 2
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 whydoidoit · May 27, 2012 at 11:31 AM 1
Share

I know this question is very old - by I wondered if you figured out you could just take off the ASCII code of a 0 from the char at position scoreStringArray[x] and save yourself that parsing... Any way, just a thought.

avatar image save · Jun 21, 2012 at 08:41 PM 0
Share

Sorry about the late answer, but that is very clever! It never got to the point of actual need of implementation so therefore there never were a crucial need of solution. :-) This could be a good way of making a digital clock display its numbers, or some other limited display area (pinball score/text display for instance).. Will try some fun stuff out with your suggestion!

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by tomka · May 29, 2012 at 06:53 AM

Mod operator

e.g.:

 int num = 123;
 int ones = 123 % 10;
 int tens = 123 % 100 - ones;
 int hundreds = 123 % 1000 - tens - ones;
 int thousands = 123 % 10000 - hundres - tens - ones;

etc.

I'm sure with a little bit of thinking you could write this up as a recursive function that will fill out an array.

Comment
Add comment · Show 2 · 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 save · Jun 21, 2012 at 08:45 PM 0
Share

I'll have a look into it, perhaps there's a clever way down that path too.

avatar image Wolfram · Jun 21, 2012 at 08:51 PM 0
Share

No need to recurse, you could do it in a simple while loop: store num%10 in the 'ones' index, divide num by 10, decrease index, repeat until num==0.

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

6 People are following this question.

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

Related Questions

How to convert a string to int array in Unity C# 1 Answer

strange cloning array beviour 1 Answer

How can I move back to Element 0 in an array to reset a loop? 1 Answer

split() for separators more than 1 characters 2 Answers

Splitting String Into Array 2 Answers


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