Objective c 将NSDate转换为NSInteger,然后再转换为NSString

Objective c 将NSDate转换为NSInteger,然后再转换为NSString,objective-c,ios,vb6,Objective C,Ios,Vb6,我正在尝试复制当前在VB中实现的功能。我需要做的是将日期转换为长整数,然后将其转换为字符串。我还需要将一个长整数转换为该整数的十六进制值,然后再转换为字符串 下面是我在VB中使用的代码 If dteCreated <> #12:00:00 AM# Then strHash = CStr(CLng(dteCreated)) If InStr(1, strHash, ".", vbTextCompare) > 0 Then strHash = Left(strHash,

我正在尝试复制当前在VB中实现的功能。我需要做的是将日期转换为长整数,然后将其转换为字符串。我还需要将一个长整数转换为该整数的十六进制值,然后再转换为字符串

下面是我在VB中使用的代码

If dteCreated <> #12:00:00 AM# Then
strHash = CStr(CLng(dteCreated))

If InStr(1, strHash, ".", vbTextCompare) > 0 Then
    strHash = Left(strHash, InStr(1, strHash, ".", 1) - 1) & "_" & Mid(strHash, InStr(1, strHash, ".", 1) + 1)
Else
    strHash = strHash & "_00000"
End If

strHash = strHash & "_" & CStr(Hex(intereq))
如果数据已创建#12:00:00 AM#那么
strHash=CStr(CLng(dteCreated))
如果InStr(1,strHash,“.”,vbTextCompare)>0,则
strHash=左(strHash,InStr(1,strHash,“,”,1)-1)和“”&中(strHash,InStr(1,strHash,“,”,1)+1)
其他的
strHash=strHash&“00000”
如果结束
strHash=strHash&“&”CStr(十六进制(intereq))
这两个变量是dteCreated(日期)和intereq(长)。这构建的是一个在我们的服务器上解码的哈希查询字符串


有人能帮助我在Objective-C中使用哪些方法来获得相同的功能吗?

您可以使用自1970年以来的参考来转换为双精度的NSTimeInterval,使用:

- (NSTimeInterval)timeIntervalSince1970
+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds
您可以将双精度转换为整数,然后只需释放几分之一秒的时间就可以将双精度转换为整数

例如:

NSDate *today = [NSDate date];
NSLog(@"today %@", today);

NSTimeInterval interval = [today timeIntervalSince1970];
NSString *hexInterval = [NSString stringWithFormat:@"%08x", (int)interval];
NSLog(@"hexInterval %@", hexInterval);

unsigned intInterval;
NSScanner *scanner = [NSScanner scannerWithString:hexInterval];
[scanner scanHexInt:&intInterval];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSLog(@"date  %@", date);
NSLog输出:

today 2011-11-15 18:34:07 +0000
hexInterval 4ec2acf0
date  2011-11-15 18:34:07 +0000

您可以使用自1970年以来的引用转换为NSTimeInterval(双精度),请使用:

- (NSTimeInterval)timeIntervalSince1970
+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds
您可以将双精度转换为整数,然后只需释放几分之一秒的时间就可以将双精度转换为整数

例如:

NSDate *today = [NSDate date];
NSLog(@"today %@", today);

NSTimeInterval interval = [today timeIntervalSince1970];
NSString *hexInterval = [NSString stringWithFormat:@"%08x", (int)interval];
NSLog(@"hexInterval %@", hexInterval);

unsigned intInterval;
NSScanner *scanner = [NSScanner scannerWithString:hexInterval];
[scanner scanHexInt:&intInterval];

NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSLog(@"date  %@", date);
NSLog输出:

today 2011-11-15 18:34:07 +0000
hexInterval 4ec2acf0
date  2011-11-15 18:34:07 +0000

你试过什么?你在文档中找到了什么?有没有什么特别的地方给你带来麻烦?你试过什么?你在文档中找到了什么?是否有一些特定的部分给你带来了麻烦?我最终使用vb创建了一个web服务来获取一个数字并返回散列字符串。当我和Obj C更亲密的时候,我会再做一次,但我现在无法证明时间的正确性。我最终使用vb创建了一个web服务来获取一个数字并返回散列字符串。当我和Obj C更亲密的时候,我会再次承担这个责任,但我现在无法证明时间的正当性。