On the problem of scanning POS QR code

Source: Internet
Author: User

stringvalue = Pos90711012511000125110003000053000011000000000002000000000000000016224161398260merch¸£¿¨êö»ú

2016-08-15 10:00:38.891 yufu_wallet[1914:1168136]-[qrviewcontroller comperterstr:] [line +] POS

907110125110001

25110003

000053

000011

000000000002

0000

000000000000

1

622416139826

0 StringValue is the name of the merchant, which has strange characters and needs to be translated into Chinese;

Tossing for 2 days,

Modelpos.mername = [self japanesetostring:stringvalue];


-(NSString *) japanesetostring: (NSString *) string{

#if 0

nsstringencoding Dec = cfstringconvertencodingtonsstringencoding (Kcfstringencodingmacjapanese);

NSData *data=[string Datausingencoding:dec];

Nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);

NSString *retstr = [[NSString alloc] Initwithdata:data Encoding:enc];

DLog (@ "retstr before =%@", retstr);

if (Mbisstringwithanytext (RETSTR))//If the scan Chinese garbled is required to deal with, otherwise do not handle

{

DLog (@ "not empty");

Nsinteger max = [string length];

Char *nbytes = malloc (max + 1);

for (int i = 0; i < max; i++)

//        {

Unichar ch = [string characteratindex:i];

Nbytes[i] = (char) ch;

//        }

Nbytes[max] = ' + ';

Retstr =[nsstring Stringwithcstring:nbytes

ENCODING:ENC];

}else{

DLog (@ "is empty");

Retstr = [self gb_2312toutf_8withstring:string];

}


return retstr;

#endif}





Multiple format support

NSString *retstr;

1,UTF8-encoded

nsstringencoding Dec = cfstringconvertencodingtonsstringencoding (Kcfstringencodingmacjapanese);

Determine the encoded format returned by byte

const char * sc = [string Cstringusingencoding:dec];

if (sc = = NULL) {//Normal code

if (Mbisstringwithanytext (RETSTR))//If the scan Chinese garbled is required to deal with, otherwise do not handle

{

retstr=string;

}else{

DLog (@ "is empty");

Retstr = [self gb_2312toutf_8withstring:string];

}



-(NSString *) gb_2312toutf_8withstring: (NSString *) string

{

Nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);

NSData *data=[string Datausingencoding:enc];

NSString *retstr = [[NSString alloc] Initwithdata:data Encoding:enc];

DLog (@ "retstr =%@", retstr);

if (RETSTR)//If the scan Chinese garbled is required to deal with, otherwise do not handle

{

DLog (@ "not empty");

Nsinteger max = [string length];

Char *nbytes = malloc (max + 1);

for (int i = 0; i < max; i++)

{

Unichar ch = [string characteratindex:i];

Nbytes[i] = (char) ch;

}

Nbytes[max] = ' + ';

Retstr =[nsstring Stringwithcstring:nbytes

ENCODING:ENC];

}else{

DLog (@ "is empty");

RETSTR = [[NSString alloc]initwithdata:[self Databyhealinggb18030stream:data] Encoding:enc];

}

return retstr;

}

}else if (IsTextUTF8 (SC, strlen (SC))) {

NSData *data=[string Datausingencoding:dec];

RETSTR = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

}else{

NSData *data=[string Datausingencoding:dec];

//

Nsstringencoding enc = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);

RETSTR = [[NSString alloc] Initwithdata:data Encoding:enc];


}


Find some C methods to judge the encoding format of the returned string


int ISGBK (const char *s, size_t NS)

{

if (ns > 2 && (uint8_t) *s >= 0x81 && (uint8_t) *s <= 0xFE

&& (

((uint8_t) * (s+1) >= 0x80 && (uint8_t) * (s+1) <= 0x7e)

|| ((uint8_t) * (s+1) >= 0xa1 && (uint8_t) * (s+1) <= 0xFE)

)

)

{

return 1;

}

return 0;

}


int IsTextUTF8 (const char* str,long length)

{

int i;

int NBYTES=0;//UFT8 can be encoded in 1-6 bytes, ASCII with one byte

unsigned char chr;

BOOL Ballascii=true; If all is ASCII, the description is not UTF-8

for (i=0;i<length;i++)

{

chr= * (str+i);

if ((chr&0x80)! = 0)//Determine if ASCII encoding, if not, indicates that it is possible that UTF-8,ASCII is encoded with 7 bits, but with one byte, the highest bit is marked as 0,o0xxxxxxx

Ballascii= false;

if (nbytes==0)//If it is not an ASCII code, it should be a multibyte character, counting bytes

{

if (chr>=0x80)

{

if (CHR>=0XFC&&CHR<=0XFD)

nbytes=6;

else if (CHR>=0XF8)

nbytes=5;

else if (chr>=0xf0)

nbytes=4;

else if (CHR>=0XE0)

nbytes=3;

else if (CHR>=0XC0)

nbytes=2;

Else

{

return false;

}

nbytes--;

}

}

else//non-first byte of multibyte character, should be 10xxxxxx

{

if ((chr&0xc0)! = 0x80)

{

return false;

}

nbytes--;

}

}

if (nbytes > 0)//rebate rules

{

return false;

}

if (BALLASCII)//If all are ASCII, the description is not UTF-8

{

return false;

}

return true;

}

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.