This time to share some of the data types there are time types of various conversion methods, with detailed comments, not much to say, directly on the code
/** *//start from 1970 to now how many seconds-(NSString *) Gettimesp {nsstring *time; NSDate *fromdate=[nsdate Date]; Time = [NSString stringwithformat:@ "%f", [FromDate timeIntervalSince1970]]; return time; }//convert timestamp to NSDate, time of conversion I do not know which country time, should be GMT-(NSDate *) Changesptotime: (nsstring*) spstring {nsdate *CONFROMTIMESP = [ NSDate datewithtimeintervalsince1970:[spstring Intvalue]]; NSLog (@ "%@", CONFROMTIMESP); return CONFROMTIMESP; }//convert timestamp to NSDate, plus time zone offset. After this conversion is Beijing time-(nsdate*) Zonechange: (nsstring*) spstring {nsdate *confromtimesp = [NSDate dateWithTimeIntervalSince1970: [Spstring Intvalue]]; Nstimezone *zone = [Nstimezone systemtimezone]; Nsinteger interval = [zone SECONDSFROMGMTFORDATE:CONFROMTIMESP]; NSDate *localedate = [Confromtimesp datebyaddingtimeinterval:interval]; NSLog (@ "%@", localedate); return localedate; }//Compare the difference between the given NSDate and the current time, the number of seconds to return-(long) Timedifference: (NSDate *) Date {nsdate *localedate = [NSDate date]; Long Differenc e =fabs ([localedate timeintervalsincedate:date]); Return DIfference; }//Will nsdate press YYYY-MM-DD HH:MM:SS format time Output-(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 YYYY-MM-DD HH:MM:SS format time to timestamp-(long) Changetimetotimesp: (NSString *) timestr {long; 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; }//Gets 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 month-date format-(NSString *) changedate {nsdate *date = [NSDate Date]; Nscalendar *calendar = [[NscalendarAlloc] Initwithcalendaridentifier:nsgregoriancalendar]; Nsinteger unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nsweekdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | 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 months%d days%d"%d seconds ", year,month,day,hour,min,sec]; NSLog (@ "%@", string); return string; *///when sending data, the 16 binary->byte array->nsdata, plus the checksum part-(NSData *) Hextobytetonsdata: (NSString *) str{int j=0; Byte Bytes[[str LENGTH]/2]; A byte array is a byte array, similar to the C language of char[], each character occupies two bytes, each digit or punctuation, and the letter occupies a byte for (int i=0;i<[str length];i++) {/** * In IPHONE/MAC development, Unichar is a two-byte long char that represents one character of Unicode. * Two single quotes can only be used with char. You can use direct write text encoding.To initialize. The following method can be used to solve the multi-character problem */int int_ch; Two-bit 16 binary number converted 10 binary number Unichar hex_char1 = [str characteratindex:i]; The first bit (high *16) int int_ch1 in the two-bit 16 binary number; if (hex_char1 >= ' 0 ' && hex_char1 <= ' 9 ') {int_ch1 = (hex_char1-48) *16; 0 Ascll-48} else if (hex_char1 >= ' A ' && hex_char1 <= ' F ') {int_ch1 = (hex_char1-55) *16; A Ascll-65} else {int_ch1 = (hex_char1-87) *16; A of Ascll-97} i++; Unichar hex_char2 = [str characteratindex:i]; Second bit (low) int INT_CH2 in two-bit 16 binary number; if (hex_char2 >= ' 0 ' && hex_char2 <= ' 9 ') {INT_CH2 = (hex_char2-48); 0 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_CH1+INT_CH2; BYTES[J] = int_ch; Put the converted number in a 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->16 number-(NSString *) Nsdatatobytetohex: (NSData *) data{Byte *bytes = (Byte *) [data bytes]; NSString *[email protected] ""; for (int i=0;i<[data length];i++) {nsstring *newhexstr = [NSString stringwithformat:@ "%x",bytes[i]&0xff]; 16 binary numbers, with 0xFF do & operationsConverts a byte value to a value of type int, and the negative value between -128~0 is 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 Chinese character string to 16 binary string-(NSString *) Chinesetohex: (nsstring*) chinesestr{nsstringencoding encodinggb18030= Cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); NSData *responsedata =[chinesestr datausingencoding:encodinggb18030]; NSString *string=[self Nsdatatobytetohex:responsedata]; NSLog (@ "%@", string); return string;} Convert kanji string to UTF8 string-(NSString *) Chinesetoutf8str: (nsstring*) chinesestr{nsstringencoding encodingUTF8 = nsutf8stringencoding; NSData *responsedata2 =[chinesestr Datausingencoding:encodingutf8]; NSString *string=[self NSDATATOBYTETOHEX:RESPONSEDATA2]; return string;} Converts a hexadecimal string into kanji-(nsstring*) changelanguage:(nsstring*) chinese{nsstring *strresult; NSLog (@ "chinese:%@", Chinese); if (chinese.length%2==0) {//second conversion 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 string must be a multiple of 4!"; NSLog (@ "%@", strresult); return NULL; } return strresult;} GBK, Kanji, gb2312,ascii code, Utf8,utf16//utf8 string converted to Kanji-(nsstring*) ChangeLanguageUTF8: (nsstring*) chinese{ NSString *strresult; NSData *data=[self Hextobytetonsdata:chinese]; Strresult=[[nsstring Alloc]initwithdata:data encoding:nsutf8stringencoding]; return strresult;} Converts a decimal number to hexadecimal-(NSString *) Tohex: (int) tmpid{nsstring *nlettervalue; NSString *str [email protected] ""; Long long int ttmpig; for (int i= 0; i<9; i++) {ttmpig=tmpid%16; TMPID=TMPID/16; Switch (ttmpig) {case 10:nlettervalue [email protected] "A"; Case 11:nlettervalue [email protected] "B"; Case 12:nlettervalue [email protected] "C"; Case 13:nlettervalue [email protected] "D"; Case 14:nlettervalue [email protected] "E"; Case 15:nlettervalue [email protected] "F"; Default:nlettervalue=[[nsstring alloc]initwithformat:@ "%lli", Ttmpig]; } str = [Nlettervalue stringbyappendingstring:str]; if (Tmpid = = 0) {break; }} return str;} Unicode converted to Kanji-(NSString *) ReplaceUnicode1: (NSString *) unicodestr{nsstring *tempstr1 = [Unicodestr stringbyreplacing occurrencesofstring:@ "\\u" withstring:@ "\\u "]; NSString *TEMPSTR2 = [tempStr1 stringbyreplacingoccurrencesofstring:@ "\" "withstring:@" \\\ ""]; NSString *TEMPSTR3 = [[@ "\" "stringbyappendingstring:tempstr2] stringbyappendingstring:@" \ ""]; NSData *tempdata = [TempStr3 datausingencoding:nsutf8stringencoding]; nsstring* returnstr = [Nspropertylistserialization propertylistfromdata:tempdata Mutabilityoption:nspropertylistimmutable Format:null Errordescription:null]; NSLog (@ "%@", [Returnstr stringbyreplacingoccurrencesofstring:@ "\\r\\n" withstring:@ "\ n"]); return [Returnstr stringbyreplacingoccurrencesofstring:@ "\\r\\n" withstring:@ "\ n"];} Iso8859-1 to Unicode encoding conversion iso8859-1 character encoding a/*-(NSString *) changeiso88591stringtounicodestring: (NSString *) iso88591st ring{nsmutablestring *srcstring = [[Nsmutablestring alloc]initwithString:iso88591string]; [Srcstring replaceoccurrencesofstring:@ "&" 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 string to array for (int I=0;i<[arr count]-1;i++) {nsstring *v = [arr objectatindex:i]; Char *c = malloc (3); int value = [Stringutil changehexstringtodecimal:v];//converts binary to decimal c[1] = value &0x00FF; C[0] = value >>8 &0x00FF; C[2] = ' + '; [Desstring appendstring:[nsstring stringwithcstring:c encoding:nsunicodestringencoding]; Free (c); } return desstring;} */
iOS time type conversion and various data types for conversion