| | |
Split / explode-like function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
Hello,
I need to decode certain packets which I need to split first. They are splitted using '@@@' (in this example I only use one '@').
I'm having this code now: (It's basic C code for a NDS platform)
Result:
packets[0] = 'test1'
packets[1] = (nothing)
packets[2] = (nothing)
What am I doing wrong? [1] and [2] should be filled too I guess!
Thanks in advance.
I need to decode certain packets which I need to split first. They are splitted using '@@@' (in this example I only use one '@').
I'm having this code now: (It's basic C code for a NDS platform)
C Syntax (Toggle Plain Text)
char buf2[256] = "test1@test2@test3@"; char packets[30][256]; int main(int argc, char ** argv) { split(buf2); //here show code for NDS } void split(char *original) { int x = 0; int i = 0; while (original[i] != '\0') { if (original[i] == '@') { packets[x][i] = '\0'; x++; } else { packets[x][i] = original[i]; } i++; PA_WaitForVBL(); } }
Result:
packets[0] = 'test1'
packets[1] = (nothing)
packets[2] = (nothing)
What am I doing wrong? [1] and [2] should be filled too I guess!
Thanks in advance.
Last edited by Darkmystery; Dec 23rd, 2008 at 12:00 pm.
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
or, you at least need another index for the characters you put into packets. When you switch to a new x index, you need to reset the output index to 0.
if the
c Syntax (Toggle Plain Text)
void split(char *original) { int x = 0; int i = 0; int jj = 0; while (original[i] != '\0') { if (original[i] == '@') { packets[x][jj] = '\0'; x++; jj = 0; } else { packets[x][jj++] = original[i]; } i++; } }
if the
PA_WaitForVBL(); is necessary, what does it do? •
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
Well, I'm programming on the Nintendo DS. ANd if I don’t put it, the program will function, MUCH faster than the screen rate and many things just won’t work at all.
And thanks guys, I got i now.
I still got another question:
I load the packet command in a string, but how do I compare it? (I'm still quite new at C)
Since this piece of code is not working:
Thanks in advance again.
And thanks guys, I got i now.

I still got another question:
I load the packet command in a string, but how do I compare it? (I'm still quite new at C)
Since this piece of code is not working:
C Syntax (Toggle Plain Text)
char cmd[3]; memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed) if (cmd == "MSG") // not working { PA_OutputText(1, 1, lineNum, packets); //show on DS screen } // not working either if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G")) { PA_OutputText(1, 1, lineNum, packets); //show on DS screen }
Thanks in advance again.
Last edited by Darkmystery; Dec 23rd, 2008 at 3:34 pm.
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
c Syntax (Toggle Plain Text)
char cmd[3]; // 3 byte array [0] [1] [2] memcpy(cmd,packets,3); // cmd contains 'MSG' (confirmed) if (cmd == "MSG") // not working // try memcmp(cmd, "MSG", 3) == 0 { PA_OutputText(1, 1, lineNum, packets); //show on DS screen } // not working either // if the last one was cmd[2] it might work if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G")) { PA_OutputText(1, 1, lineNum, packets); //show on DS screen }
c Syntax (Toggle Plain Text)
//... memcpy... //... use memcmp(...,...,...); //... //...to compare two string : int s_compare(const char *str1, const char *str2) { if (strlen(str1) != strlen(str2)) return 1; for (;*str1!='\0';*str1++,*str2++) if (*str1 != *str2) return 1; return 0; } // what's this...?? if ((cmd[0] == "M") && (cmd[1] == "S") && (cmd[3] == "G")) { PA_OutputText(1, 1, lineNum, packets); //show on DS screen } // maybe, if (cmd[0] == 'M')
Last edited by cikara21; Dec 23rd, 2008 at 4:29 pm.
![]() |
Similar Threads
- Help me with my myspace clone (PHP)
- PHP 'explode()' in C++ ? (C++)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Warning: mysql_num_rows(): (PHP)
- html file content into a variable (PHP)
- Search Script help (PHP)
- Inserting array into cookie (PHP)
Other Threads in the C Forum
- Previous Thread: How do you check if arrow keys are pressed?
- Next Thread: Any function to restart from the beginning of the same line?
Views: 846 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h






