c/c++语言开发共享在C中将字符串解析为long

我在python中编写了一个服务器,在C中编写了客户端。

python服务器将“1 1000 n”发送到C客户端。

收到此字符串的C函数应将其解析为两个long int。

void receive_job() { char tmp_buffer[256]; long start, stop; recv(sock, tmp_buffer, 255, 0); /* here I wonder how I can parse tmp_buffer to set start and stop values. */ } 

我对C不是很精通,所以我很感激对此发表评论。

    查找sscanf手册页 – 但检查返回值!

    看看strtol()或strtoul(),具体取决于您的具体数据类型:

     char end; long x = strtol(tmp_buffer,&end,10); long y = strtol(end+1,NULL,10); 

    使用strtol

     char data[] = "1 1000n"; char *next, *err; long a = strtol(data, &next, 10); long b = strtol(next, &err, 10); 

    最好在调用recv之前使用memset清除tmp_buffer或者至少在其中添加空字节。 否则它会包含垃圾,如果传输错误,你可能会解析它。

      以上就是c/c++开发分享在C中将字符串解析为long相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/523050.html

      (0)
      上一篇 2020年12月11日
      下一篇 2020年12月11日

      精彩推荐