INT64类型的数转为c 把string转为int用什么函数好

本帖子已过去太久远了,不再提供回复功能。How to convert string to int64_t? [如何将字符串转换int64_t?] - 问题-字节技术
How to convert string to int64_t?
如何将字符串转换int64_t?
问题 (Question)
How to convert program parameter from argv to int64_t? atoi() is suitable only for 32 bit integers.
如何将程序参数argv对int64_t?atoi()仅适用于32位整数。
最佳答案 (Best Answer)
A C99 conforming attempt.
[edit] employed @R. correction
// Note: Typical values of SCNd64 include "lld" and "ld".
#include &inttypes.h&
#include &stdio.h&
int64_t S64(const char *s) {
int scanned = sscanf(s, "%" SCNd64 "%c", &i, &c);
if (scanned == 1)
if (scanned & 1) {
// TBD about extra data found
int main(int argc, char *argv[]) {
if (argc & 1) {
int64_t i = S64(argv[1]);
printf("%" SCNd64 "\n", i);
一个C99的尝试。[编辑]采用“R.校正// Note: Typical values of SCNd64 include "lld" and "ld".
#include &inttypes.h&
#include &stdio.h&
int64_t S64(const char *s) {
int scanned = sscanf(s, "%" SCNd64 "%c", &i, &c);
if (scanned == 1)
if (scanned & 1) {
// TBD about extra data found
int main(int argc, char *argv[]) {
if (argc & 1) {
int64_t i = S64(argv[1]);
printf("%" SCNd64 "\n", i);
答案 (Answer) 2
converts it to a long long which is usually a 64-bit int.
转换到一个long long这通常是一个64位的int。
答案 (Answer) 3
There are a few ways to do it:
strtoll(str, NULL, 10);
This is POSIX C99 compliant.
you ca which has the following prototype:
strtoimax(const char *str, char **endptr, int base);
This is nice because it will always work with the local intmax_t ... This is C99 and you need to include &inttypes.h&
有很多方法去做:
strtoll(str, NULL, 10);
这是POSIX C99标准。你也可以使用strtoimax;其原型: strtoimax(const char *str, char **endptr, int base);
这是好的因为它总是与当地intmax_t工作…这是C99和你需要包括&inttypes.h&
答案 (Answer) 4
Doing this 100% portably is a little bit tricky. long long is required to be at least 64 bits, but need not necessarily be twos complement, so it might not be able to represent -0x7fffffffffffffff-1, and thus using strtoll could have a broken corner case. The same issue applies to strtoimax. What you could do instead is consume leading space (if you want to allow leading space) and check for the sign first, then use strtoull or strtoumax, either of which is required to support values up to the full positive range of int64_t. You can then apply the sign:
unsigned long long x = strtoull(s, 0, 0);
if (x & INT64_MAX || ...)
int64_t y = negative ? -(x-1)-1 :
This logic is written to avoid all overflow cases.
这样做,100%便是有点棘手。long long需要至少64位,但不一定是补码,所以它可能无法代表-0x7fffffffffffffff-1因此,与使用strtoll能有一个缺角的情况下。同样的问题strtoimax。你所能做的反而是消费主导的空间(如果你想让领导空间)和检查的第一个迹象,然后使用strtoull或strtoumax是的,这是需要支持的值到全阳性范围int64_t。你可以把标志:unsigned long long x = strtoull(s, 0, 0);
if (x & INT64_MAX || ...)
int64_t y = negative ? -(x-1)-1 :
这种逻辑是书面避免溢流情况下。
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:OC 常用数据类型之间的转换 - 光荣在于平淡,卓越在于专注 - ITeye技术网站
博客分类:
1: NSString
&==& NSInteger
NSInteger转化 NSString类型:
stringWithFormat:
, NSInteger];
NSString转化 NSInteger类型:
NSInteger = [ NSString
2 : NSString &==& NSNumber
NSNumber 转换 NSString 类型:
NSNumber *indexNum = [NSNumber numberWithInt:index];
UITextView *login = (UITextView*)[[UIView alloc] viewWithTag:1];
NSNumberFormatter *formater = [[NSNumberFormatter alloc ] init];
[login setText:[formater stringFromNumber:indexNum]];
NSString 转换为 NSNumber 类型:
[NSString stringWithFormat]
3、 int,NSInteger,NSUInteger,NSNumber
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用
NSInteger,因为这样就不用考虑设备是32位的还是64位的
NSUInteger是无符号的
,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类
。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[
NSNumber numberWithInt
4.NSString 与 int ,float,double,NSInteger之间的转换
*teststring = @"32.54"
i = [teststring intValue
i1 = [teststring floatValue
i2 = [teststring doubleValue
i3 = [teststring integerValue
*tostring = nil
tostring = [
stringWithFormat:
@"tostring-int:%@",tostring);
tostring = [
stringWithFormat:
@"tostring-float:%@",tostring);
tostring = [
stringWithFormat:
@"tostring-double:%@",tostring);
NSNumber转NSString:
假设现有一NSNumber的变量A,要转换成NSString类型的B
方法如下:
NSNumberFormatter
* numberFormatter = [[NSNumberFormatter
B = [numberFormatter stringFromNumber
[numberFormatter release
nsstring和float 还有int之间的转换
NSString *tempA = @"123";
NSString *tempB = @"456";
1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2,字符转int
int intString = [newString intValue];
3,int转字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4,字符转float
float floatString = [
floatValue];
5,float转字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
NSString *a = [[NSString alloc] initWithString : @"5, 10, 2, 0, 0, 0.01, 1, 0.05, 0, 0, 0"];
NSArray *bullteData = [a componentsSeparatedByString:@","];
[a release];
for (int i = 0; i & [bullteData count]; i++) {
NSLog(@"%f",[[NSString stringWithFormat:@"%@",[bullteData objectAtIndex:i]] floatValue]);
NSString *a = [[NSString alloc] initWithString : @"5, 10, 2, 0, 0, 0.01, 1, 0.05, 0, 0, 0"];
NSArray *bullteData = [a componentsSeparatedByString:@","];
[a release];
for (int i = 0; i & [bullteData count]; i++) {
NSLog(@"%f",[[NSString stringWithFormat:@"%@",[bullteData objectAtIndex:i]] floatValue]);
将字符串存到数组中,在读取的时候转换成需要的类型。
浏览 12064
浏览: 304232 次
来自: 湖南娄底
[[color=brown]color=yellow][url ...
NSString &==& NSNumber 之前 ...
果然能解决问题,感谢分享
很细致。。
哥哥,为什么我的没有破解成功,总是提示许可文件丢失!!QQ:3 ...温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1282)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'StrToInt64:将一个整数型字符串转换为Int64类型的数值。',
blogAbstract:' 声明:function StrToInt64 ( IntegerString : string ) : Int64; 描述:StrToInt64函数将一个整数型字符串IntegerString比如123,转换为一个Int64类型。 它支持正号与负号数字,支持十六进制数字(前缀为¥或0X)。 注意,Delphi7和某些其他版本的Delhpi,可以忽略十六进制的X前面的0。 备注:如果FloatString字符串中有错误,比如尾部有空格,或含有小数点,或无效的十进制,或无效的字符,则会抛出EConvertError例外。
{转换十进制和十六进制数字} var
A, B, C,',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}}

我要回帖

更多关于 c 把string转为int 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信