样题: POJ 2105

+----------------+
|      strtol             |
+----------------+

i.e. string to long

long int strtol(const char *nptr, char **endptr, int base)
strtol()会将nptr指向的字符串,根据参数base,按权转化为long int, 然后返回这个值。
参数base的范围为2~36,和0;它决定了字符串以被转换为整数的权值。
可以被转换的合法字符依据base而定,举例来说,当base为2时,合法字符为‘0’,‘1’;base为8时,合法字符为‘0’,‘1’,……‘7’;base为10时,合法字符为‘0’,‘1’,……‘9’;base 为16时,合法字符为‘0’,‘1’,……‘9’,‘a’,……‘f’;base为24时,合法字符为‘0’,……‘9’,‘a’,……‘n’,base为36时,合法字符为‘0’,……‘9’,‘a’,……‘z’;等等。其中,不区分大小写,比如,‘A’和‘a’会都会被转化为10。
当字符合法时,‘0’,……‘9’依次被转换为十进制的0~9,‘a’,……‘z’一次北转换为十进制的10~35。
strtol()函数检测到第一个非法字符时,立即停止检测,其后的所有字符都会被当作非法字符处理。合法字符串会被转换为long int, 作为函数的返回值。非法字符串,即从第一个非法字符的地址,被赋给*endptr。**endptr是个双重指针,即指针的指针。strtol()函数就是通过它改变*endptr的值,即把第一个非法字符的地址传给endptr。

多数情况下,endptr设置为NULL, 即不返回非法字符串。
下面看几个例子:
------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 2));
printf("%s\n", stop);
输出结果:
2
379cend$3
-------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 8));
printf("%s\n", stop);
输出结果:
543
9cend$3
--------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 10));
printf("%s\n", stop);
输出结果:
10379
cend$3
-------------------------------------------------------
char buffer[20]="10379cend$3";
char *stop;
printf("%d\n",strtol(buffer, &stop, 16));
printf("%s\n", stop);
输出结果:
17005006
nd$3
另外,如果base为0,且字符串不是以0x(或者0X)开头,则按十进制进行转化。如果base为0或者16,并且字符串以0x(或者0X)开头,那么,x(或者X)被忽略,字符串按16进制转化。如果base不等于0和16,并且字符串以0x(或者0X)开头,那么x被视为非法字符。
例如:
-------------------------------------------------------
char buffer[20]="0x31da6c";
char *stop;
printf("%d\n",strtol(buffer, &stop, 0));
printf("%s\n", stop);
输出结果(stop为空):
3267180

-------------------------------------------------------
char buffer[20]="0x31da6c";
char *stop;
printf("%d\n",strtol(buffer, &stop, 13));
printf("%s\n", stop);
输出结果:
0
0x31da6c
-------------------------------------------------------

最后,需要说明的是,对于nptr指向的字符串,其开头和结尾处的空格被忽视,字符串中间的空格被视为非法字符。
例如:
-------------------------------------------------------
char buffer_1[20]="10379c";
char buffer_2[20]="      10379c        ";
char buffer_3[20]="      10      379c        ";
printf("%d\n",strtol(buffer_1,NULL,0));
printf("%d\n",strtol(buffer_2,NULL,0));
printf("%d\n",strtol(buffer_3,NULL,0));
输出结果为:
10379
10379
10
--------------------------------------------------------

POJ 2105    IP Address

代码Description

Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decimal format for an IP address is form by grouping 8 bits at a time and converting the binary representation to decimal representation. Any 8 bits is a valid part of an IP address. To convert binary numbers to decimal numbers remember that both are positional numerical systems, where the first 8 positions of the binary systems are:

27   26  25  24  23   22  21  20 128 64  32  16  8   4   2   1 

Input

The input will have a number N (1<=N<=9) in its first line representing the number of streams to convert. N lines will follow.

Output

The output must have N lines with a doted decimal IP address. A dotted decimal IP address is formed by grouping 8 bit at the time and converting the binary representation to decimal representation.

Sample Input

4
00000000000000000000000000000000
00000011100000001111111111111111
11001011100001001110010110000000
01010000000100000000000000000001 

Sample Output

0.0.0.0
3.128.255.255
203.132.229.128
80.16.0.1

代码:

#include<string.h>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;

int main()
{
    int tt;
    scanf("%d",&tt);    
    for(int i=0;i<tt;i++)
    {
        for(int j=0;j<4;j++)
        {
            char ss[9];
            scanf("%8s",ss);
            int ans = strtol(ss,0,2);
            if(j<3)
            {
                printf("%d.",ans);
            }
            else
            {
                printf("%d\n",ans);
            }                    
        }            
    }        
    return 0;
}

strtol 函数详解相关推荐

  1. C库函数之 strtol函数详解

    strtol函数详解 函数声明 base为0的情况 endptr的妙用 函数声明 long int strtol (const char* str, char** endptr, int base); ...

  2. C/C++之strtol函数详解

    翻了翻旧代码,发现以前对strtol这个函数不太了解,特此分析一下. C 库函数strtol原型为: long int strtol(const char *str, char **endptr, i ...

  3. C语言网络编程:accept函数详解

    文章目录 前言 函数描述 代码实例 如何得到客户端的IP 和 端口号 前言 当使用tcp服务器使用socket创建通信文件描述符,bind绑定了文件描述符,服务器ip和端口号,listen将服务器端的 ...

  4. 【FFmpeg】函数详解(三)

    FFmpeg函数详解 14.av_write_frame 15.av_interleaved_write_frame 16.av_write_trailer 17.avio_close 18.av_i ...

  5. 【FFmpeg】函数详解(二)

    FFmpeg函数详解 9.av_dump_format 10.avio_open 11.avformat_write_header 12.avcodec_send_frame 13.avcodec_r ...

  6. 【FFmpeg】函数详解(一)

    FFmpeg函数详解 一.错误码相关 1.AVERROR 2.av_strerror 3.其他错误码解释 二.编解码 1.获取编解码器 2.申请.释放上下文环境 3.打开编码器avcodec_open ...

  7. 【ES6】Generator函数详解

    [ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...

  8. mysql的聚合函数综合案例_MySQL常用聚合函数详解

    一.AVG AVG(col) 返回指定列的平均值 二.COUNT COUNT(col) 返回指定列中非NULL值的个数 三.MIN/MAX MIN(col):返回指定列的最小值 MAX(col):返回 ...

  9. python平方数迭代器_对python中的高效迭代器函数详解

    python中内置的库中有个itertools,可以满足我们在编程中绝大多数需要迭代的场合,当然也可以自己造轮子,但是有现成的好用的轮子不妨也学习一下,看哪个用的顺手~ 首先还是要先import一下: ...

最新文章

  1. 九度OJ 朋友圈 并查集
  2. js的nextSibling,属性兼容IE和FF等浏览器
  3. java的成员方法_java编程中的成员方法是什么?
  4. 用Quartus II Timequest Timing Analyzer进行时序分析 :实例讲解
  5. stl clocklist 查找元素_C++算法竞赛中常用的STL
  6. SAP Spartacus的site context配置
  7. C语言实现随机发纸牌
  8. oracle-01122,oracle ORA-01200ORA-01110ORA-01122
  9. python切割图片文字_Python+opencv 实现图片文字的分割的方法示例
  10. 【论文阅读】Efficient Net
  11. 惊爆:「文言文」编程语言,可谓年度最骚语言也
  12. 2.5数字传输系统2.6宽带接入技术
  13. 青春(2010-05-28 04:30:39)韩寒
  14. 又整理了一些面试相关的:视频教程,面试经验,简历模板,写简历的技巧等
  15. iPhone白苹果修复工具
  16. Packet Tracer学习小结(基本SwitchPort VLan)
  17. 那些你不知道的RK3288人脸识别方案
  18. redis服务器 本地连接
  19. 遗忘线代知识,导致应力第三不变量对应力的导数求不出来
  20. 手把手教你从零开始做一个好看的 APP

热门文章

  1. linux 串口 loopback,友善NanoPC T2 4418开发板Linux下串口回环测试 -申嵌
  2. 获取浏览器具体的下载链接地址,通过迅雷下载
  3. 学好英语的七个规则(下)
  4. 【数据结构面试常见问题】
  5. 拍卖小程序直播功能扩展之翻转摄像头、美颜
  6. 复旦大学陈怡然:文本摘要的跨数据集迁移研究
  7. TCP丢包原因、解决办法
  8. 【安装】Hadoop2.8.0搭建过程整理版
  9. 透过代理加速 Github 访问
  10. 商城项目-SPU和SKU数据结构