有三个功能请求高手能提供点意见
1.要将一个数字从十进制转成二进制并以MSB方式并且能选择以2 Bytes或4 Bytes方式输出~不知道有没有更简易的写法~或是程式中有没有Bug?~
程序代码:
int DecToHex(int Array_start, unsigned int Source,unsigned char *Result, int How_Byte)//10进位转16进位(MSB) { int TempTotalCount1=0, TempTotalCount2=0, TempTotalCount3=0,TempTotalCount4=0; if (How_Byte == 4) { if ((Source / 256) >= 1) { if ((Source / 65536) >= 1) { if ((Source / 16777216) >= 1) { Result[Array_start + 3] = (Source / 16777216); //最高位数字(最右) TempTotalCount1 = Result[Array_start + 3]; Result[Array_start + 2] = ((Source / 65536) - (TempTotalCount1 * 256)); //次高位数字(右) TempTotalCount2 = Result[Array_start + 2]; Result[Array_start + 1] = ((Source / 256) - (TempTotalCount1 * 65536) - (TempTotalCount2 * 256)); //次次高位数字(右) TempTotalCount3 = Result[Array_start + 1]; Result[Array_start + 0] = ((Source / 1) - (TempTotalCount1 * 16777216) - (TempTotalCount2 * 65536)-(TempTotalCount3*256));//最低位数字(最左) TempTotalCount4 = Result[Array_start + 0]; } else { if (Array_start == 262704) { Result[262700] = 5; Result[262704] = 5; Result[262705] = 5; Result[262706] = 5; Result[262707] = 5; } Result[Array_start + 3] = 0; Result[Array_start + 2] = (Source / 65536); TempTotalCount2 = Result[Array_start + 2]; Result[Array_start + 1] = ((Source / 256) - (TempTotalCount2 * 256)); Result[Array_start] = (Source - (256 * (Source / 256))); } } else { Result[Array_start + 3] = 0; Result[Array_start + 2] = 0; Result[Array_start + 1] = (Source / 256); Result[Array_start] = (Source - (256 * (Source / 256))); } } else { Result[Array_start + 3] = 0; Result[Array_start + 2] = 0; Result[Array_start + 1] = 0; Result[Array_start] = Source; } } else if (How_Byte == 2) { if ((Source / 256) >= 1) { Result[Array_start + 1] = (Source / 256); Result[Array_start] = (Source - (256 * (Source / 256))); } else { Result[Array_start + 1] = 0; Result[Array_start] = Source; } } else if (How_Byte == 1) { Result[Array_start] = Source; } Array_start++; return Array_start; }
2.要将原本Ascii Code转成Binary Code并且在整段字符串(二进制)前加上该字符串(二进制)的长度
不知道有沒有更簡易的寫法~或是程式中有沒有Bug?
程序代码:
int StringToHex(int K_start, int K_end, int j_start, int flag, char **part1,unsigned char *TitleTemp)//字串转16进位码 { unsigned int m,j,n,S_len=0; int k; char *temp; j = j_start; //TitleTemp的开始位址 k=K_start; //part1的开始位址 for (k = K_start;k<=K_end;k++) //part1的目前位址 { m = 0; temp=part1[k]; if (strcmp(temp," ") != 0) { if(flag!=1) { S_len = strlen(temp); TitleTemp[j] = S_len; for (n=j+1;n<=(j+1)+S_len-1;n++) { TitleTemp[n] = (int)temp[m]; m = m + 1; } j=j+S_len+1; } else { TitleTemp[j] = (int)temp[0]; j = j + 1; } } else { TitleTemp[j] = 0; j = j + 1; } } j_start=j; return j; }
3.要将原本MSB写法的二进制2Bytes或4Bytes长度字节计算出十进制的数字
不知道有沒有更簡易的寫法~或是程式中有沒有Bug?
程序代码:
unsigned int sum(int start,int end,unsigned char *TitleTemp)//MSB 2Bytes或4Bytes计算 { unsigned int mode; long Number,Number1,Number2,Number3,Number4; Number=0,Number1=0,Number2=0,Number3=0,Number4=0; mode=end-start+1; if(mode==2) { long Number1=(long)TitleTemp[end]*256; Number1=Number1&0x0000ffff; long Number2=(long)TitleTemp[end-1]*1; Number2=Number2&0x000000ff; Number=Number1+Number2; return Number; } else if(mode==4) { long Number1=(long)TitleTemp[end]*16777216; Number1=Number1&0xffffffff; long Number2=(long)TitleTemp[end-1]*65536; Number2=Number2&0x00ffffff; long Number3=(long)TitleTemp[end-2]*256; Number3=Number3&0x0000ffff; long Number4=(long)TitleTemp[end-3]*1; Number4=Number4&0x000000ff; long Number=Number1+Number2+Number3+Number4; return Number; } return 0; }