//对hexstring类型的数据进行编码 void encode_hexstring(MyBinaryString *msg, String str) { unsigned char * binstr; unsigned char binvalue; unsigned long len; int i,k; printf("\n In function encode_hexstring() \n"); //输入的字符串中含有前后双引号和H,将它们去掉 len = str?strlen(str):0; for(i=0;i<len-3;i++){ str[i]=str[i+1]; } str[i]='\0'; len=strlen(str);//the new length if(len%2==0){//偶数的话与处理octetstring相同 binstr =(unsigned char *)malloc(len); char2hex_encode(msg,str,binstr); binary_string_append_bytes(msg, binstr, len/2); }else{ //基数的话先处理前面的偶数个字符 binstr =(unsigned char *)malloc(len); //先把最后一个字符保存起来 binvalue = str[i-1]; str[i-1] = '\0'; char2hex_encode(msg,str,binstr); binary_string_append_bytes(msg, binstr, len/2); //处理最后一个字节,不够的用0填充 len += 1; if(((binvalue-'0')>=0)&&((binvalue-'9')<=0)) { binvalue = (binvalue-'0')*16; } else if(((binvalue-'A')>=0)&&((binvalue-'F')<=0)) { binvalue = (binvalue-'A'+10)*16; } else if(((binvalue-'a')>=0)&&((binvalue-'f')<=0)) { binvalue = (binvalue-'a'+10)*16; } else { tci_assert(0, "Wrong hex string: the value should be between 0~9, a~f"); } binary_string_append_char(msg,binvalue); } printf("\n Leave function encode_hexstring() \n"); } |