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 sdgd · Jul 22, 2013 at 06:08 PM · c#characterintegergrabspecific

how to grab a specific int without converting it to string?

it should be quick and easy question

I'm trying to grab a specific integer without converting it to string

let say a number 341 and I want to grab 2. and I want to put it in to else value so that value would be 4 in the end and not 341 nor 143, ...

I wanted to go the long way and do some research and seems I got a bit rusty over few months not programming

 string x = ID.ToString();
 int a = int.Parse(x.Substring(0,1));
 int b = int.Parse(x.Substring(1,1));
 int c = int.Parse(x.Substring(2,1));

like this example

but not converting it to string

thanks in advance

sice some people need whole script just don't say you got confused inside here

 using UnityEngine;
 using System.Collections;
 
 public class ExtendMe : MonoBehaviour {
     public int ID = 0;
     public int ExtendNumber = 0;
     public GameObject Parent;
     private Weapons ParentScript;
     void Start () {
         ParentScript = Parent.GetComponent("Weapons") as Weapons;
     }
     void Update () {
         if (ParentScript.Openb && ParentScript.OpenCounti >= ExtendNumber){
             if (! (transform.position.y <= Parent.transform.position.y -0.9f) ){
                 transform.position = Vector3.Lerp(transform.position, Parent.transform.position + new Vector3(0,-1-(ExtendNumber*0.6f), 0.5f), Time.deltaTime * 2);
             }
             else{
                 transform.position = Vector3.Lerp(transform.position, Parent.transform.position + new Vector3(0,-1-(ExtendNumber*0.6f)), Time.deltaTime * 3);
             }
             // we tell to send another extension
             if (transform.position.y < 2.5f && ParentScript.OpenCounti == ExtendNumber){
                 ParentScript.OpenCounti = ExtendNumber+1;
             }
         }
         else {
             transform.position = Vector3.Lerp(transform.position, Parent.transform.position + new Vector3(0, 0, 0.5f), Time.deltaTime * 3);
         }
     }
     
     void OnMouseOver () {
         ParentScript.Timef = Time.time + 0.4f;
         // right hand
         if (Input.GetMouseButtonDown(1)){
             ChangeEquippmentF(1);
         }
     }
     // left hand
     void OnMouseDown () {
         ChangeEquippmentF(0);
     }
     
     // 0 = left hand
     // 1 = right hand
     // 2 = center (body or what ever)
     void ChangeEquippmentF(int LRC){
         
         string x = ID.ToString();
         int a = int.Parse(x.Substring(0,1));
         int b = int.Parse(x.Substring(1,1));
         int c = int.Parse(x.Substring(2,1));
         
         if (a == 1 && b >= 2){
             if (b == 2){
                 GameMainController.GlovesSTATICi = ID;
             }
             if (b == 3){
                 GameMainController.HeadSTATICi = ID;
             }
             if (b == 4){
                 GameMainController.ArmorSTATICi = ID;
             }
         }
         else if (LRC == 0){
             GameMainController.LeftHandSTATICi = ID;
         }
         else if (LRC == 1){
             GameMainController.RightHandSTATICi = ID;
         }
     }
 }


I changed ID accordinatelly to different box left boxes have 1 on first field first row has 0 and in that row there's 8 possibilities

so from 100 - 258 numbers each ID has it's unique characteristic

it's better than doing 150 array length

Comment
Add comment · Show 3
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 clunk47 · Jul 22, 2013 at 06:23 PM 1
Share

I'd be more than happy to help if you'd post your whole script. I need to know what ID is defined as, and how you have it here with no methods or classes, I don't know what exactly you're going for.

avatar image sdgd · Jul 22, 2013 at 06:25 PM 0
Share

ID is simply a number (int)

avatar image vividhelix · Jul 22, 2013 at 11:37 PM 0
Share

There are a bunch of different solutions to this problem, but frankly I'm wondering if it's really an issue to begin with. Are you running into performance limitations? Having an array of 150 elements of type int will consumer 150x4 = 600 bytes which is the equivalent (typically) of a string with 300 characters. On any platform these days that's far from reaching any limitations.

There's a common pitfall in program$$anonymous$$g called "premature optimization". You're basically trading code simplicity for performance (and most times the performance gain is not noticeable).

In your case, I would strongly consider going towards a solution that is easier to understand and doesn't require parsing strings and splitting ints. Something like an object that you can pass around having all of those things as attributes (and if you want, you can easily serialize it as well should you wish to store it).

3 Replies

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

Answer by clunk47 · Jul 22, 2013 at 07:06 PM

This Example will return the second digit of your int.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     int x = 341;
     int y;
     string s;
     char[] chars;
     
     void Awake()
     {
         s = x.ToString();
         chars = s.ToCharArray();
         y = int.Parse (chars[1].ToString());
         print (y);
     }
 }



EDIT:

If you're looking for less code, this example will have the same output.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     int i = 342;
     char c;
     
     void Start()
     {
         c = i.ToString()[1];
         print (c);    
     }
 }
Comment
Add comment · Show 7 · 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 clunk47 · Jul 22, 2013 at 07:16 PM 2
Share

It's either this, or the answer @brianruggieri gave you.

avatar image clunk47 · Jul 22, 2013 at 07:18 PM 2
Share

Because you have to split the int into separate characters, then use another int that will parse the character array. At least this is the easiest and most efficient way I can think of, I'm no genius. C# works in mysterious ways lol XD

avatar image sdgd · Jul 22, 2013 at 07:32 PM 2
Share

ok I'm accepting this answer since it does work and since I see it's not possible to get it from in to int without converting

thanks for your effort guys gave few thumbs up for your efforts + your karma shows you know aloot about program$$anonymous$$g and if you say it's not possible there's aloot of % that it really isn't

avatar image vividhelix · Jul 22, 2013 at 11:21 PM 2
Share

C# string are indexable, so you can actually just write i.ToString()[1], no need to call ToCharArray.

avatar image clunk47 · Jul 22, 2013 at 11:29 PM 1
Share

Thanks for the clarification. Will edit answer.

Show more comments
avatar image
2

Answer by roojerry · Jul 22, 2013 at 06:54 PM

If I am understanding you correctly, this answer should be what you are looking for .

for those who don't want to follow the link:

here's the function:

 public int nthdigit(int x, int n)
 {
    while (n-- > 0) {
      x /= 10;
    }
    return (x % 10);
 }

here's how it would be called:

 nthdigit(1234567, 1);

that call would return the integer 6 without the use of strings

Comment
Add comment · Show 11 · 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 sdgd · Jul 22, 2013 at 07:07 PM 0
Share

could be, ... but we don't have char nthdigit or I'm doing it wrong, what I can say is that question is same what I want but for unity C#

 char nthdigit(int x, int n)
 {
     while (n--) {
         x /= 10;
     }
     return (x % 10) + '0';
 }
avatar image roojerry · Jul 22, 2013 at 07:13 PM 0
Share

That is a C# function that you would call by passing it the integer you want to split ("ID"), and the position of the integer you want to return. The function returns a character, but you could easily parse that as an integer

avatar image sdgd · Jul 22, 2013 at 07:17 PM 0
Share

ok since it seems it's not possible I'll wait a few hours than I'll close the question and put 1 vote to yours as it's closest but not what I want as I don't want convertions

just from int to int

avatar image roojerry · Jul 22, 2013 at 07:24 PM 0
Share

modified it for you

 int nthdigit(int x, int n)
     {
         while (n-- > 0) {
             x /= 10;
         }
         return (x % 10);
     }

now returns an integer

However, if you read the answer and were extremely concerned about the speed of the operation, the second function given may be even faster.

avatar image clunk47 · Jul 22, 2013 at 08:37 PM 1
Share

True, fixed the title.

Show more comments
avatar image
0

Answer by vividhelix · Jul 22, 2013 at 08:17 PM

Depending on what you want to do with that number, you can just get the character from the string. In c# char is a numeric type.

For example:

 string blah="341";
 char first = blah[0]; //first will equal '3', notice the ' for character
 char second = blah[1];
 char third = blah[2];
 int firstNumber = first - '0'; //first number will be 3

Because char is a numeric type, you can do any numeric operations with it. By subtracting '0' you're basically getting the number you're looking for.

Hope this makes sense :)

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Simple Data Parsing from Text Files 1 Answer

Prevent a specific RB to influence another, but still collide with everything 0 Answers

Velocity for movement 0 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