- Home /
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
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.
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).
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);
}
}
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
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
C# string are indexable, so you can actually just write i.ToString()[1], no need to call ToCharArray.
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
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';
}
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
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
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.
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 :)
Your answer
Follow this Question
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