From this line you need to pull 12 and continue to work with him.

"Принято в Socket->ReceiveBuf() 12 байт:" 
  • one
    Can. Universal approach (but not the fact that the most effective): go through the loop on the source line and check each character. If the number (just do not reinvent the bicycle, there is an isdigit() ) - add it to the end of the line for the result. - PinkTux
  • one
    Or - go to the first digit, then use sscanf() , atoi() etc. - PinkTux

2 answers 2

Something like

 #define ERROR_VALUE -1 char * s = "Принято в Socket->ReceiveBuf() 12 байт:"; int getFirstInt(char * s) { char * c; for(c = s; *c != 0; ++c) { if (isdigit(*c)) return atoi(c); } return ERROR_VALUE; } int main(int argc, const char * argv[]) { printf("Get %d bytes\n",getFirstInt(s)); } 

True, negative values ​​are not being processed here ...

  • With negative values, it is easy to fix - return c > s && c[-1] == '-' ? -atoi(c) : atoi(c); return c > s && c[-1] == '-' ? -atoi(c) : atoi(c); - avp

will return c[-1] == '-' ? -atoi(c) : atoi(c); and so work return c[-1] == '-' ? -atoi(c) : atoi(c); return c[-1] == '-' ? -atoi(c) : atoi(c); because at the first iteration and so c> s

  • Just at the first iteration for (c = s; ...) c equals s - avp
  • There are no iterations yet - c equals s - this is the first entry into the loop Then ++ with and with is used. I am wrong? Those value from 1 iteration will be sizeof (* s) greater than s. I am sorry, your code was of course. But it works the way I wrote above - I checked. - skorp
  • Are you about the getFirstInt() function code in the @Harry response? The increment (++ c) is performed at the very end of each iteration. At the beginning of the first c == s , so if the first character in s digit, then in the expression c[-1] == '-' ? ... c[-1] == '-' ? ... access to non- s memory will occur, which is generally not correct (UB). - avp
  • Yes of course. You're right. I hurried. We just consider the line that is, without a universal approach. Yes - on the first c = s. And c99 allows the next element to look after the last element (in the array)? (More precisely, to determine the pointer) or on the previous one? - skorp
  • then int getFirstInt(char * s) { char * c; for(c = s+1; *c != 0; ++c) { if (isdigit(*c)) return c[-1] == '-' ? -atoi(c) : atoi(c); } return ERROR_VALUE; } int getFirstInt(char * s) { char * c; for(c = s+1; *c != 0; ++c) { if (isdigit(*c)) return c[-1] == '-' ? -atoi(c) : atoi(c); } return ERROR_VALUE; } int getFirstInt(char * s) { char * c; for(c = s+1; *c != 0; ++c) { if (isdigit(*c)) return c[-1] == '-' ? -atoi(c) : atoi(c); } return ERROR_VALUE; } - skorp