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)

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.

Recommended Answers

All 7 Replies

You might want to take a look at strtok.

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.

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?

And don't forget to terminate the last token when break the loop...

if the PA_WaitForVBL(); is necessary, what does it do?

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:

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.

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
}
//... 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')

Thanks alot guys, got it all working now. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.