IOS time type conversion and various data types Conversion

Source: Internet
Author: User
Tags binary to decimal

IOS time type conversion and various data types Conversion

In this article, we will share some data types and time-type conversion methods, with detailed comments. If you don't talk about them, go directly to the code.

 

/*** Time conversion part * // number of seconds elapsed since January 1, 1970-(NSString *) getTimeSp {NSString * time; NSDate * fromdate = [NSDate date]; time = [NSString stringWithFormat: @ "% f", [fromdate timeIntervalSince1970]; return time ;}// convert the timestamp to NSDate, I don't know which country the conversion time is. It should be Greenwich Mean Time-(NSDate *) changeSpToTime :( NSString *) spString {NSDate * confromTimesp = [NSDate dateWithTimeIntervalSince1970: [spString intValue]; NSLog (@ "% @", confromTimesp); r Eturn confromTimesp;} // converts the timestamp to NSDate, and adds the time zone offset. This conversion is followed by Beijing Time-(NSDate *) zoneChange :( NSString *) spString {NSDate * confromTimesp = [NSDate dateWithTimeIntervalSince1970: [spString intValue]; NSTimeZone * zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: confromTimesp]; NSDate * localeDate = [confromTimesp interval: interval]; NSLog (@ "% @", localeDate); return localeDate;} // compares the time difference between the given NSDate and the current time, and returns the number of seconds of difference -(Long) timeDifference :( NSDate *) date {NSDate * localeDate = [NSDate date]; long difference = fabs ([localeDate timeIntervalSinceDate: date]); return difference ;} // output NSDate in yyyy-MM-dd HH: mm: ss format-(NSString *) nsdateToString :( NSDate *) date {NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; NSString * string = [dateFormat stringFromDate: date]; NSLog (@ "% @", string); return string ;}// convert the time in the yyyy-MM-dd HH: mm: ss format to the timestamp-(long) changeTimeToTimeSp :( NSString *) timeStr {long time; NSDateFormatter * format = [[NSDateFormatter alloc] init]; [format setDateFormat: @ "yyyy-MM-dd HH: mm: ss "]; NSDate * fromdate = [format dateFromString: timeStr]; time = (long) [fromdate timeIntervalSince1970]; NSLog (@" % ld ", time); return time ;} // obtain the current system's yyyy-MM-dd HH: mm: ss format time -( NSString *) getTime {NSDate * fromdate = [NSDate date]; NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat: @ "yyyy-MM-dd HH: mm: ss "]; NSString * string = [dateFormat stringFromDate: fromdate]; return string ;}// convert the current time to the format of year, month, and day-(NSString *) changeDate {NSDate * date = [NSDate date]; NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; N SInteger unitFlags = calendar | NSDayCalendarUnit | calendar | NSHourCalendarUnit | calendar | NSSecondCalendarUnit; NSDateComponents * comps = [calendar components: unitFlags fromDate: date]; NSInteger year = [comps year]; NSInteger month = [comps month]; NSInteger day = [comps day]; NSInteger hour = [comps hour]; NSInteger min = [comps minute ]; NSInteger sec = [comps second]; NSString * string = [NSString stringWithFormat: @ "% d: % d seconds ", year, month, day, hour, min, sec]; NSLog (@ "% @", string); return string;} * // when sending data, hexadecimal number-> Byte array-> NSData, plus the verification code section-(NSData *) hexToByteToNSData :( NSString *) str {int j = 0; byte bytes [[str length]/2]; // Byte array is a Byte array, which is similar to char [] in C language. Each Chinese Character occupies two bytes, each number, punctuation, or letter occupies one byte for (int I = 0; I <[str length]; I ++) {/*** on the iphone/ma In c development, unichar is a two-byte char that represents a unicode character. * Two single quotes can only be used for char. You can directly write the text encoding to initialize the SDK. The following method can be used to solve the problem of multiple characters */int int_ch; // The number of hexadecimal unichar hex_char1 = [str characterAtIndex: I] After two hexadecimal numbers are converted. /// the first (high * 16) int int_second in the two hexadecimal numbers; if (hex_char1> = '0' & hex_char1 <= '9 ') {int_rectangle = (hex_char1-48) * 16; // 0 Ascll-48} else if (hex_char1> = 'A' & hex_char1 <= 'F ') {int_l = (hex_char1-55) * 16; // Ascll-65} else {int_ch1 = (hex_char1-87) * 16; /// Ascll-97} I ++ OF a; unichar hex_char 2 = [str characterAtIndex: I]; // The second (low) int int_ch2 in the two hexadecimal numbers; if (hex_char2> = '0' & hex_char2 <= '9') {int_ch2 = (hex_char2-48 ); /// 0 of Ascll-48} else if (hex_char2> = 'A' & hex_char2 <= 'F') {int_ch2 = hex_char2-55; /// A's Ascll-65} else {int_ch2 = hex_char2-87; // a's Ascll-97} int_ch = int_ch + int_ch2; bytes [j] = int_ch; /// put the converted number into the Byte array // if (j = [str length]/2-2) {// int k = 2; // Int_ch = bytes [0] ^ bytes [1]; // while (k // int_ch = int_ch ^ bytes [k]; // k ++; //} // bytes [j] = int_ch; //} j ++;} NSData * newData = [[NSData alloc] initWithBytes: bytes length: [str length]/2]; NSLog (@ "% @", newData); return newData;} // when receiving data, NSData-> Byte array-> hexadecimal number-(NSString *) NSDataToByteTohex :( NSData *) data {Byte * bytes = (Byte *) [data bytes]; NSString * hexStr = @ ""; for (int I = 0; I <[data length]; I ++) {NSStr Ing * newHexStr = [NSString stringWithFormat: @ "% x", bytes [I] & 0xff]; // hexadecimal number, perform the & operation with 0xff to convert the byte value to the int type value, and also convert-128 ~ The negative values between 0 are converted to positive values. If ([newHexStr length] = 1) {hexStr = [NSString stringWithFormat: @ "% @ 0% @", hexStr, newHexStr];} else {hexStr = [NSString stringWithFormat: @ "% @", hexStr, newHexStr] ;}} NSLog (@ "hexStr: % @", hexStr); return hexStr ;} // convert a Chinese character string to a hexadecimal string-(NSString *) chineseToHex :( NSString *) chineseStr {NSStringEncoding encodingGB18030 = bytes (kCFStringEncodingGB_18030_2000); NSData * respo NseData = [chineseStr dataUsingEncoding: encodingGB18030]; NSString * string = [self NSDataToByteTohex: responseData]; NSLog (@ "% @", string); return string ;} // convert a Chinese character string to a UTF8 string-(NSString *) chineseToUTf8Str :( NSString *) chineseStr {enencodingutf8 = Beijing; NSData * responseData2 = [chineseStr dataUsingEncoding: encodingUTF8]; NSString * string = [self NSDataToByteTohex: responseData2]; Return string;} // convert a hexadecimal string to a chinese character-(NSString *) changeLanguage :( NSString *) chinese {NSString * strResult; NSLog (@ "chinese: % @", chinese); if (chinese. length % 2 = 0) {// NSData * newData = [self hexToByteToNSData: chinese]; unsigned long encode = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000 ); strResult = [[NSString alloc] initWithData: newData encoding: encode]; NSLog (@ "strResult: % @ ", StrResult);} else {NSString * strResult = @" is assumed to be a conversion of Chinese characters. The length of the passed string must be a multiple of 4! "; NSLog (@" % @ ", strResult); return NULL;} return strResult;} // GBK, Chinese character, GB2312, ASCII code, UTF8, UTF16 // UTF8 string to chinese characters-(NSString *) changeLanguageUTF8 :( NSString *) chinese {NSString * strResult; NSData * data = [self hexToByteToNSData: chinese]; strResult = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; return strResult;} // convert the decimal number to hexadecimal-(NSString *) ToHex :( int) tmpid {NSString * nLetterValue; NSString * str = @ ""; long int ttmpig; for (int I = 0; I <9; I ++) {ttmpig = tmpid % 16; tmpid = tmpid/16; switch (ttmpig) {case 10: nLetterValue = @ "A"; break; case 11: nLetterValue = @ "B "; break; case 12: nLetterValue = @ "C"; break; case 13: nLetterValue = @ "D"; break; case 14: nLetterValue = @ "E"; break; case 15: nLetterValue = @ "F"; break; default: nLetterValue = [[NSString alloc] initWithFormat: @ "% lli", ttmpig];} str = [nLetterValue stringByAppendingString: str]; if (tmpid = 0) {break;} return str;} // convert Unicode to Chinese characters-(NSString *) replaceUnicode1 :( NSString *) unicodeStr {NSString * tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString: @ "\ u" withString: @ "\ U"]; NSString * tempStr2 = [tempStr1 limit: @ "\" "withString: @" \\\ ""]; NSString * tempStr3 = [[@ "\" "stringByAppendingString: tempStr2] stringByAppendingString: @" \ ""]; NSData * tempData = [tempStr3 dataUsingEncoding: Required]; NSString * returnStr = [using propertyListFromData: tempData mutabilityOption: Invalid format: NULL errorDescription: NULL]; NSLog (@ "% @", [returnStr failed: @ "\ r \ n" withString: @ "\ n"]); return [returnStr stringByReplacingOccurrencesOfString: @ "\ r \ n" withString: @ "\ n"];} // A/*-(NSString *) changeISO88591StringToUnicodeString :( NSString *) iso8859-1 to unicode conversion iso8859-1 character encoding *) optional {NSMutableString * srcString = [[NSMutableString alloc] initWithString: iso88591String]; [srcString failed: @ "&" withString: @ "&" options: NSLiteralSearch range: NSMakeRange (0, [srcString length])]; [srcString replaceOccurrencesOfString: @ "& # x" withString: @ "" options: NSLiteralSearch range: NSMakeRange (0, [srcString length])]; NSMutableString * desString = [[NSMutableString alloc] init]; NSArray * arr = [srcString componentsSeparatedByString: @ ";"]; // cut the string into an array for (int I = 0; I <[arr count]-1; I ++) {NSString * v = [arr objectAtIndex: I]; char * c = malloc (3 ); int value = [StringUtil changeHexStringToDecimal: v]; // convert binary to decimal c [1] = value & 0x00FF; c [0] = value> 8 & 0x00FF; c [2] = '\ 0'; [desString appendString: [NSString stringWithCString: c encoding: NSUnicodeStringEncoding]; free (c);} return desString ;}*/


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.