typedefintBOOL;#defineTRUE 1;#defineFALSE 0;#defineUint_max 0xFFFFFFFF/* Maximum unsigned int value */enumscale24asciival{Sav_aadis= +,//Lowercase letters and uppercase ASCII difference valuesSav_chintdis = -,//character ' 0 ' ASCII code value};Static Const Charscale24[ -] = {'0','1','2','3','4','5','6','7','8','9','A','B', 'C','F','H','K','M','P','S','T','U','W','X','Y'};//determine if the 24 encoded characters are validBOOL IsScale24 (Const CharCH, unsignedint*nval);/*24 binary encoded string converted to a 32-bit unsigned integer, where: parameter: str 24 encoding string length 24 binary encoded string lengths, if < 0 is str with ' "" End code conversion return code, if code = = NULL ignores return code return value: (value) The converted return value, whether the conversion succeeded from code. */unsignedintC24TOU32 (Const Char* STR,intLengthint*code) {unsignedintresult =0; Const Char* PStr =str; unsignedintNval =0;//bit value intCodeidx =1;//The ordinal of an illegal character in the encoded string if(Code! = NULL) *code =0; if((pstr==null) | | ((pstr=="") && (length==0)) ) { if(Code! =NULL)*code =-1;//conversion failed, possible parameter error or string is empty } Else { if(Length <0)//str ends with ' /' { while(*pstr! =' /') { if(IsScale24 (*PSTR, &nval)) { if((Uint_max-nval)/ -) < result)//conversion failed (overflow) { if(Code! =NULL)*code =Codeidx; Break; } Elseresult= result* -+ Nval;//Carry and add-on value } Else { if(Code! =NULL)*code = Codeidx;//the conversion failed with the code character (containing the first character) to be a non-24-binary encoding Break; } codeidx++; PStr++; } } Else { while(Codeidx <=length) { if(IsScale24 (*PSTR, &nval)) { if((Uint_max-nval)/ -) < result)//conversion failed (overflow) { if(Code! =NULL)*code =Codeidx; Break; } Elseresult= result* -+ Nval;//Carry and add-on value } Else { if(Code! =NULL)*code = Codeidx;//the conversion failed with the code character (containing the first character) to be a non-24-binary encoding Break; } codeidx++; PStr++; } } } returnresult;}/*determine if the 24 encoded characters are valid, where: parameter: Ch to determine the character Nval ch if legal, then nval for ch corresponding to the 24 binary character bit value (' A '->10, ' B '->11, ' C '->12 ...) Return value: (value) ch legal->true, illegal->false. */BOOL IsScale24 (Const CharCH, unsignedint*nval) {BOOL result=FALSE; unsignedintNbegin =Ten;//Two-point find ch, initial start positionUnsignedintNend = at;//two points find ch, initial end positionUnsignedintNmid = -;//two points find ch, initial binary positionUnsignedintNoffset =0; Charchscale=0; if(Ch >='0') && (Ch <='9'))//' 0 ' ... ' 9 ' { *nval = (unsignedint) (ch-Sav_chintdis); Result=TRUE; } Else if(Ch >='A'&& CH <='y') { if(Ch >'Z')//Ch is a lowercase letterNoffset =Sav_aadis; //' A (a) ' ... ' Y (y) ' two points find ch while(Nbegin <=nend) {Chscale= Scale24[nmid] +Noffset; if(Chscale = =ch) { *nval =Nmid; Result=TRUE; Break; } Else if(Chscale >ch) nend= Nmid-1; ElseNbegin= Nmid +1; Nmid= (Nbegin + nend)/2; } } returnresult;}View Code
Comments:
1. if ((pstr==null) | | | (pstr== "") && (length==0))) The wrong bug should be: if ((pStr = = NULL) | | (length = = 0) | | (Length < 0) && (*pstr = = '))) 2. if ((uint_max-nval)/24 < result) to determine the bug that the overflow is flawed; 3. IsScale24 (const char CH, unsigned int* nval) lookup efficiency is not good enough, you can put the lowercase letters in the array, all the two-point search, so as to reduce the judgment; 4. Rating: 75 points
24 binary encoded string converted to 32-bit unsigned integer (C language Implementation)