#529 - jenswa - Wed Jan 08, 2003 2:07 pm
Here is a little question:
i know there is a substring command in c/c++ (like in almost every language).
i need it to split a two digit number like 79 in a 7 and a 9,
does someone now the awnser?
perhaps a little example too make sure i get it?
thanx
Jenswa
_________________
It seems this wasn't lost after all.
#535 - Touchstone - Wed Jan 08, 2003 3:00 pm
Actually the the string functions arent parts of the C/C++ language, they are functions implemented in the standard libraries. So to use these functions on your gba project you must make sure you have the standard libraries compiled for the gba.
Provided you have the standard libraries you can use strncpy to copy parts of one string to another. Check out http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strncpy.2c_.wcsncpy.2c_._mbsncpy.asp for more info on the strncpy function and pray to your local god or godess that the function you are using was implemented the same way. This is one way of doing what you want to do in C anyways.
Code: |
char* pszNumber = "47";
char* pszFour = (char*)calloc(2, sizeof(char));
char* pszSeven = (char*)calloc(2, sizeof(char));
strncpy(pszFour, &pszNumber[0], 1);
strncpy(pszSeven, &pszNumber[1], 1); |
pszFour and pszSeven will now be pointers to two separate zero-terminated strings, pszFour will say "4" and pszSeven will say "7" (of course :)
_________________
You can't beat our meat
#545 - Burre - Wed Jan 08, 2003 4:53 pm
You should also be able to mask and bitshift.
_________________
"The best optimizer is between your ears..."
#556 - jenswa - Wed Jan 08, 2003 8:16 pm
Yes, those kind of things,
from programming are things
i need to learn/study.
_________________
It seems this wasn't lost after all.
#626 - AnthC - Thu Jan 09, 2003 2:05 am
Hi
A combination of the divide and modulus operators should do the trick for you.
int thousands=(number/1000)%10;
int hundreds =(number/100)%10;
int tens =(number/10)%10;
int units =(number/1)%10;
See the pattern developing?
There are faster methods that don't use a divide - usally using repeated subtraction and a multiply by 10 each time, which I won't go into here.
AnthC
#686 - jenswa - Thu Jan 09, 2003 7:05 pm
well i gues i can do that,
it seems nice and neat,
i was just looking at a source code
which uses the same trick.
thnx
JJ
_________________
It seems this wasn't lost after all.
#688 - Touchstone - Thu Jan 09, 2003 7:11 pm
Jenswa, keep in mind that AnthC's method doesn't split up strings, it split up integers.
_________________
You can't beat our meat
#689 - jenswa - Thu Jan 09, 2003 7:16 pm
Yeah i see the difference,
but i want to use it for score, so
that method works fine for me.
About the substring, i probably need to
have a lib or something for my compiler,
i use devkitadvance, but i won't use
substring for now.
thnx
_________________
It seems this wasn't lost after all.
#700 - Touchstone - Thu Jan 09, 2003 9:03 pm
Ok. I'd recommend staying away from standard librarys on the gba so you are better of using AnthC's method anways. :) Avoiding stdlib's is probably just me being old-fashioned.
_________________
You can't beat our meat
#1000 - animension - Mon Jan 13, 2003 6:57 am
If you have a char* called mystr containing "47" couldn't you just do the following?
unsigned char four, seven;
four = mystr[0];
seven = mystr[1];
#1011 - Touchstone - Mon Jan 13, 2003 11:07 am
animension, yeah you could do that. But then four wouldn't be the value 4, it would be the character '4'. See the difference? '4' is actually 54. If you would want to have the int value of '4' you could do
Code: |
char szString[3] = "47";
Four = szString[0] - '0'; |
_________________
You can't beat our meat