PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Working w/ strings (again)

Working w/ strings (again)

I'm really having a hard time on this but I thought I'd ask my concerns here because there are some of you who are really more experienced on this than me.

I am currently working on a word game on the side together w/ a big project and I have a couple of quick questions when working w/ strings as mentioned below. I would appreciate it if you can lend a hand and shed me some light on this topic. :)

1. I have this code:

Code:
TCHAR temp[8];

_tcscpy(temp, TEXT("ERICK");


Q: How do I parse temp such that I will be able to blit each letter on the screen separately? i.e. Blit("E"), Blit("R")...

2. In VB, there's a function there called "Mid" where you can copy a number of letters from a string by specifiying its position and length, is there an easy way to do this in C? Any API?

3. Lastly, I was able to convert a character to its ascii value by casting (int) on it. How do I convert an ascii value to its corresponding character now? (A --> 65, 65 --> A?)

Thanks!

Erick
In answer to your first question:

for (int i = 0; i < 8; i++) {
displayChar(temp[i]);
}

whre displayChar is whatever function you will use to plot. The main point is that TCHAR's can be accessed just like an array of char's (actually, I might have the data type wrong here, but it's just an array, that's the point).

For the second, check out strncpy. It's not exactly the same as mid, but you should be able to just specify a pointer to the character position in your string to start copying at, and then the proper length, and you should be able to get the same effect. There is a wide-character version as well, which is probably what you need.

The answer to your third question, unless I'm missing something, is just cast to char. I believe doing (char)65 will do what you want, but how you intent to use it will probably make a difference. Putting it into a string perhaps? Something like:

TCHAR temp[5] = TEXT("1234");
temp[3] = (char)65;

I'm pretty sure that will work. Actually, I'm not even sure you need to bother casting it.
I can offer an answer for the pulling out certain chars:

Code:

TCHAR temp[8], t[2];

_tcscpy(temp, TEXT("ERICK");

t[1] = 0; //NULL terminate the t string

for (int i = 0; i < 2; i++)
{
t[0] = temp[i];
Blit(t);
}


That will loop through each letter in temp and place it in the t string. Then, you can Blit ( or whatever ) with the t string. You only have to NULL terminate it once, but you must NULL terminate. If you don't whatever reads the string will keep on going until it finds a NULL character in the memory after the t array and you may or may not get screwy characters and/or memory access violations.

Now, if your display function can handle single chars, just do what fzammetti said. If it needs strings, my method is one way to do it.

There are also several string handling functions built in to the standard library. I believe that there isn't a function that does exactly what Mid does, but one can easily be written.

Code:

int main(void)
{
char temp[10], t[5];

strcpy(temp, "some text");

midsz(t, temp, 2, 5);
//t whould now be "me te"

return 0;
}

int midsz(char *dest, char *source, int start, int num)
// copies num chars starting at start from source to dest
{
int i, c;

for (i = 0, c = start; i <= num; i++, c++)
{
dest[i] = source[c];
}

dest[i] = 0; //null terminate

return i; //return num chars copied
}


If fzammetti's answer didn't, HTH
I'm pretty sure that will work. Actually, I'm not even sure you need to bother casting it.


AFAIK a tchar is just an unsigned short, so you wouldn't really need the typecast. It's probably a good practice to do so, though.
Character types are integer and will convert implicitly without an explicit typecast.

I'm not sure why you want to go from int to char. Where are the ints coming from? In code, you can just make character literals like 'A' (instead of using 65).

You can do pointer arithmetic on your "string" which is just an array of characters, which is just a pointer. So if you have ptr which points to a string, then (ptr+5) points into the string starting at the sixth character.
damian wrote:
AFAIK a tchar is just an unsigned short, so you wouldn't really need the typecast. It's probably a good practice to do so, though.


A WCHAR is an unsigned short. A TCHAR is either a WCHAR or a char depending on whether or not you are building for UNICODE. The idea with the TCHAR types (LPCTSTR, LPTSTR, tcscpy, tcscat, etc) is that you no longer have to worry about UNICODE / ANSI builds. If you only want to support UNICODE then use the WCHAR types. If you only want to support ANSI then use char. If you want to be a good coder then use TCHAR and do it properly so it will build for both.

Dan East
Guys, thank you very much for the various tips! My word parser is working great now! :)

Superman: Thanks for spending time on that "mid" function. It'll definitely come handy.

Dan: To be able to run the app both on windows desktop and the pocket pc, that means we should use TCHAR, right?

I know I'll probably have follow up "string-related" questions in the future but this will do for now and I will post them as I learn this stuff. :)

Now I only need to find an encoder to help me out on my word database... :)
egarayblas wrote:
Dan: To be able to run the app both on windows desktop and the pocket pc, that means we should use TCHAR, right?


That’s the gist of it.

Dan East