Linux C 实现atoi函数

itoa 及其实现与sprintf /*************************************************************************************** itoa与sprintf函数 linux c 字符串处理函数 itoa函数及atoi函数,c语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符 串。以下是用itoa()函数将整数转 换为字符 阅读详情

目的:编写一个字符串转整型的接口

/**************************************
* Description   : 实现atoi函数的作用
* Editor        :  Donkey
* Date          :  2019-5-4 23:01
**************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int myatoi(char* str)
{
        if(NULL == str)
        {   
                return 0;
        }   
        int flag = 1;//正负数的标志位,如果是负数,最后乘-1.
    
        int s = 0;

        while(*str == ' ')
        {   
                //去除开头空格
                str++;
        }

        if(*str == '+' || *str == '-')
        {
                if(*str == '-')
                {
                        flag = -1;
                }
                str++;
        }
        else if(*str < '0' || *str > '9')
        {
                s = 2147483647;
                return s;
        }

        while(*str != '\0' && *str >= '0' && *str < '9')
        {
                //主要部分,减掉'0'是为了ASCII的码转为数字
                s = s * 10 + *str - '0';
                str++;

        }
        return s*flag;
}


int main(int argc,char **argv)
{
        int num;
        char * str = "123";
        num = myatoi(str);
        printf("[%d %s] str = '%s' , num = %d\n",__LINE__,__FUNCTION__,str,num);

        char *str2 = "a123";
        num = myatoi(str2);
        printf("[%d %s] str2 = '%s' , num = %d\n",__LINE__,__FUNCTION__,str2,num);

        char *str3 = "1 23";
        num = myatoi(str3);
        printf("[%d %s] str3 = '%s' , num = %d\n",__LINE__,__FUNCTION__,str3,num);

        char *str4;
        num = myatoi(str4);
        printf("[%d %s] str4 = '%s' , num = %d\n",__LINE__,__FUNCTION__,str4,num);
        return 0;

 

 

输出情况如下:

 

Android PackageManagerService 总结(一)应用市场下载安装apk流程 前言: 本篇文章是对系统包安装流程的总结,基于Android12 上 com.android.packageinstaller 源码的分析,第三方应用商城(华为商城,小米商城,应用宝,豌豆荚,酷安等)下载安装应 用,在普通安装和静默安装app两种方式下,对代码流程的梳理和讲解。 触发安装: 当你在商城界面中点击安装按钮,应用会自动下载,下载完成后就会调起系统安装应用的界面,此种触发方式一般是通过Intent 隐式调用的,我们先阅读如下代码: ...... 阅读详情

相关推荐

生信物种注释软件Kraken2+Bracken的安装与使用

一、软件安装与数据库下载 1.Kraken2安装 Kraken2是一个基于k-mer算法的高精度宏基因组序列分类软件,能够快速的将测序reads进行物种分类。 使用conda一键安装,要先安装好anaconda或者miniconda conda install -y kraken2 输入krakek2 --help 出现如下所示帮助文档,则表示安装成功。 2.Bracken安装 bracken是一种从宏基因组数据中高度准确的计算物种丰度的统计方法。 同样使用conda一键安装 conda install -

jileiqwd的博客 1万+

Linux C之atoi()函数

1.首先man下 ATOI(3)       Linux Programmer's Manual                   ATOI(3) NAME  atoi, atol,atoll, atoq - convert a string to an integer SYNOPSIS #include   int atoi(const char *nptr); long

yueyaquanBoy的专栏 3300

若依(RuoYi)项目里,Quartz定时任务踩坑实录:并发控制、事务回滚与异常处理

本文深入解析若依(RuoYi)项目中Quartz定时任务的并发控制、事务回滚与异常处理三大核心问题。通过实战案例揭示@DisallowConcurrentExecution注解的局限性和Redis分布式锁的应用,详细讲解Spring事务集成方案及多数据源事务管理,并提供生产级监控体系搭建指南,帮助开发者规避Quartz在生产环境中的常见陷阱。

weixin_28731223的博客 413

linux c 数字字符串互转 相关函数 atoi、atof、atol、atrtod、strtol、strtoul

一、atoi函数(将字符串转换成整型数) 定义函数 int atoi(const char * nptr); 函数说明 atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。 返回值:返回转换后的整型数。 附加说明 atoi()与使用strtol(nptr,(char**)NULL,1...

whatday的专栏 4492

从零搭建UVM验证环境:新手入门实践指南

在数字芯片验证领域,SystemVerilog和UVM(Universal Verification Methodology)是构建高效验证环境的核心技术。其原理基于面向对象编程和事务级建模,通过组件化架构实现验证的复用性和自动化。这一方法学的技术价值在于显著提升验证效率与质量,广泛应用于处理器、通信芯片等复杂设计的验证场景。本文聚焦于UVM验证环境的搭建实践,通过一个加法器实例,详细解析了uvm_driver、uvm_monitor、uvm_sequence等关键组件的协同工作机制,并提供了基于Quest

weixin_30015371的博客 449

《深入掌握MongoDB数据库》 - 专栏介绍和目录

本专栏系统讲解MongoDB从基础到高级的全栈知识,涵盖安装配置、核心操作、性能优化、安全运维等核心内容。基础部分包括CRUD操作、数据类型、查询语法等入门知识;进阶部分深入索引优化、聚合查询、事务处理等高级特性;核心原理解析存储引擎与数据格式;安全运维则聚焦访问控制、认证机制、审计日志等企业级实践。通过实战案例与最佳实践,帮助开发者全面掌握MongoDB在不同场景下的应用,适合各层次技术人员系统学习。

数据知道的博客 3000

linux C编程8-字符串处理

字符串应用编程

qq_37932504的博客 2216

Pre-suite 开发之MFi功能搭建

Pre-suite 固件中只有带无线硬件的Ezairo 7160sl才具备无线通信能力,其无线功能包括标准BLE 5.2 低功耗蓝牙功能以及非标准的低功耗无线音频流功能。Ezairo 7160sl 支持3种低功耗无线音频流协议:来自APPLE的MFi,来自Google的ASHA以及来自onsemi的remote mic。其中MFi音频流走的G.722编码的Mode1,速率为64kbps;而非MFi音频流走的G.722的Mode3,速率为48kbps.

SnowYang2017的博客 1506

嵌入式Linux系统编程 — 4.6 atoi、strtol等函数实现字符串与数字转换

atof()strtod()strtof()和strtold()是C语言标准库中用于将字符串转换为浮点数的函数。atof() 函数:将字符串转换为双精度浮点数 (doublestrtod() 函数:将字符串转换为双精度浮点数 (double),可以获取剩余字符串。strtof() 函数:将字符串转换为单精度浮点数 (float),可以获取剩余字符串。strtold() 函数:将字符串转换为长双精度浮点数 (),可以获取剩余字符串。str:指向待转换的以字符串形式表示的浮点数的字符数组。

几度春风里的博客 1506

【LaTex】3.5 数据分析

简述了LaTex柱状图、饼图、折线图的画法。

你的指尖有改变世界的力量 5891

第29章:PCL PrincipalCurvatures 曲率估计

PrincipalCurvatures(主曲率估计)是一种用于计算点云表面曲率的算法。曲率描述了曲面局部的弯曲程度,是表面几何特征的重要指标。在点云处理中,估计表面的曲率对于形状分析、物体识别、表面重建等任务非常重要。主曲率是曲面上某一点的最大和最小曲率方向。通过计算点云的主曲率,我们可以获得点云中每个点的局部形状特征。通常情况下,主曲率包括两个主要参数:最大主曲率(最大曲率方向上的曲率)和最小主曲率(最小曲率方向上的曲率)。优点缺点提供了对点云表面局部形状的深入分析。

yumeiguo的博客 214

Linux学习(函数)

Linux需要用到的函数(持续补充...)

weixin_47273943的博客 1855

YOLOv8推理程序

YOLOv8单独推理,有时候我们自定义的模块算子无法正常转换为其他框架,而且需要做成应用,因此需要一个单独推理的程序,返回的是识别后的照片还有各个类别及其对应数量。文章最后给出Flask封装为Server以及调用的实例还有Client请求代码,支持几十路多线程并发,只需加载一次模型,后边推理即可。

AIOT魔法师 699

linux中atoi函数的实现 值得借鉴,C语言模拟实现atoi函数的实例详解

C语言模拟实现atoi函数的实例详解atoi函数,主要功能是将一个字符串转变为整数,例如将“12345”–>12345。但在实现过程中,我们难免会因为考虑不够全面而漏掉比较重要的几点,今天就总结一下实现atoi函数需要注意的地方。1.指针为NULL2.字符串为空字符串3.空白字符4.正号与负号问题5.溢出问题6.异常字符处理接下来看代码:(具体几种问题处理都在代码的注释中说明)#define...

weixin_36254564的博客 395

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

atoi(c89)

<!-- .mjn * {font-family:Courier New; padding:0px; margin:0px} .mjn h1 {font-size:1.2em; font-weight:bold; border-bottom:1px solid green; margin:10px 0px} .mjn h2 {font-size:1em;

MJN 8354

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

【造轮子】实现 atoi、itoa 函数

1. atoi 函数 1.1 函数简介 atoi 函数为 C 库函数之一,用于把字符串转换为对应的整形十进制数字。 原型:int atoi (const char* str); 所属头文件:<stdlib.h> 功能:把 str 指向的字符串转换为对应的整形十进制数字。 返回:转换得到的数值。 1.2 函数说明 在原 atoi 函数中,如果字符串含非数字字符即非 ‘0’ -...

Kong 352

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Linux下 itoa,atoi 函数的实现

atoi和itoa是C中两个常见的函数,功能是进行字符和数字来回转换,原型如下: int atoi(const char *nptr); //nptr是要转换的字符串,返回转换后的整数。 char *itoa( int value, char *string, int radix); //value是要转换的整数,string是接受的转换后字符串,返回的也是这个, radix是几进制,如2,10分别表示二进制,10进制。 itoa函数的头文件是stdio.h, 它并不是一个标准的C函数...

Briwisdom的博客 1万+

Linux kernel内核中的atoi,itoa等函数 .

【整理】Linux内核中的atoi,itoa等函数 对于普通应用程序,可以通过包含头文件stdlib.h和stdio.h,string.h等,然后调用需要的itoa(),atoi()等函数, 但是对于Linux内核来说,无法使用那些C库中的函数。 对此Linux内核里有了相应的实现: 对于atoi()用simple_strtol() ,simple_strtoul()等函数替代; 对于i

tyl_355402的专栏 3533

Linux kernel内核中的atoi,itoa等函数

http://hi.baidu.com/serial_story/blog/item/fcec44ec707dd5dd2e2e21ed.html   【整理】Linux内核中的atoi,itoa等函数 对于普通应用程序,可以通过包含头文件stdlib.h和stdio.h

samantha_sun的专栏 4817

linux c数字转字符串函数,Linux常用C函数—字符串转换篇

Linux 常用C 函数—字符串转换篇atof (将字符串转换成浮点型数)相关函数 atoi ,atol ,strtod ,strtol ,strtoul定义函数 double atof(const char *nptr);函数说明返回值程序实例:/* 将字符串a 与字符串b 转换成数字后相加*/ #include#includeint main() { char *a="-100.23";cha...

weixin_42520025的博客 1020

Linux内核中的atoi,itoa等函数

对于普通应用程序,可以通过包含头文件stdlib.h和stdio.h,string.h等,然后调用需要的itoa(),atoi()等函数, 但是对于Linux内核来说,无法使用那些C库中的函数。 对此Linux内核里有了相应的实现: 对于atoi()用simple_strtol() ,simple_strtoul()等函数替代; 对于itoa(),用snprintf()。

shuningzhang的专栏 1333

6.23作业

结果-->>不记得查漏-->>相关指令分类、用户指令、磁盘指令补缺-->>后续-->>记忆 5min 30min 12h 1day 2day 4day 7day 15day。

m0_56844552的博客 624

网络编程知识点总结(6)

socket服务端和客户端实现双发聊天 网络编程知识点总结1)socket()函数 int socket(int domain, int type, int protocol); (2)bind()函数: IP号端口号与相应描述字赋值函数 int bind(int sockfd, const struct sockaddr *addr,socklen t addrlen); (3)listen()函数:监听设置函数 int listen(int sockfd, int backlog); (4

wei_Embedded_z的博客 171

嵌入式基础面经

文章目录memmove 和 memcpy的区别 又到面试季了,很多小伙伴们准备跃跃欲试,那么我们准备好了吗?下面将总结一些面试题,巩固一下基础。 memmove 和 memcpy的区别 他们的作用是一样的,区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确。 void *memcpy(void *dest, const void *src,...

zhaoyun_zzz的专栏 1958

【MIT6.S081】Lab1: Xv6 and Unix utilities(详细解答版)

实验内容网址:https://xv6.dgs.zone/labs/requirements/lab1.html。

lllllllllljg的博客 1920

atoi与itoa

linux下有atoi函: ATOI(3)                    Linux Programmer’s Manual                    ATOI(3) NAME        atoi, atol, atoll, atoq - convert a string to an integer SYNOPSIS        #includ

FOLORO的博客 546
上一篇: expect脚本实现自动化操作
下一篇: 获取tcp缓冲区大小 - C++
Tobiu
博客等级 码龄10年 51粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值