String library.

Dependents:   CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more

Committer:
AkinoriHashimoto
Date:
Thu Jun 11 01:50:08 2015 +0000
Revision:
0:058192932ad2
Child:
1:7c89cd414311
string library.; I2A, A2I, compare.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:058192932ad2 1 #include "StrLib.h"
AkinoriHashimoto 0:058192932ad2 2
AkinoriHashimoto 0:058192932ad2 3 long A2I(string str, int base) // Error; rtn '-1'
AkinoriHashimoto 0:058192932ad2 4 {
AkinoriHashimoto 0:058192932ad2 5 long ans= 0, tmp= 0;
AkinoriHashimoto 0:058192932ad2 6 int leng= str.size(); // String Length;
AkinoriHashimoto 0:058192932ad2 7 char chr[1];
AkinoriHashimoto 0:058192932ad2 8 for(int i=0; i<leng; i++) {
AkinoriHashimoto 0:058192932ad2 9 // string -> char
AkinoriHashimoto 0:058192932ad2 10 strcpy(chr, str.substr((leng-1)-i, 1).c_str());
AkinoriHashimoto 0:058192932ad2 11
AkinoriHashimoto 0:058192932ad2 12 if('0'<=chr[0] && '9'>=chr[0])
AkinoriHashimoto 0:058192932ad2 13 tmp= chr[0]-'0';
AkinoriHashimoto 0:058192932ad2 14 else if('A'<=chr[0] && 'F'>=chr[0])
AkinoriHashimoto 0:058192932ad2 15 tmp= chr[0]-'A'+0x0A; // ex. C=12= 2(C-A)+ 10(0x0A)
AkinoriHashimoto 0:058192932ad2 16 else if('a'<=chr[0] && 'f'>=chr[0])
AkinoriHashimoto 0:058192932ad2 17 tmp= chr[0]-'a'+0x0A;
AkinoriHashimoto 0:058192932ad2 18 else
AkinoriHashimoto 0:058192932ad2 19 return -1;
AkinoriHashimoto 0:058192932ad2 20 // UART_BT.printf("%s;%d\n", chr[0], tmp);
AkinoriHashimoto 0:058192932ad2 21 ans+= tmp* (int)pow((double)base,(double)i);
AkinoriHashimoto 0:058192932ad2 22 // UART_BT.printf("ans%d\n", ans);
AkinoriHashimoto 0:058192932ad2 23 }
AkinoriHashimoto 0:058192932ad2 24 return ans;
AkinoriHashimoto 0:058192932ad2 25 }
AkinoriHashimoto 0:058192932ad2 26 string I2A(int num, int base, int digitNum)
AkinoriHashimoto 0:058192932ad2 27 {
AkinoriHashimoto 0:058192932ad2 28 char tmpChr[33];
AkinoriHashimoto 0:058192932ad2 29 string format= "%";
AkinoriHashimoto 0:058192932ad2 30
AkinoriHashimoto 0:058192932ad2 31 // Digit Num.
AkinoriHashimoto 0:058192932ad2 32 if(digitNum > 0) { // 0以外
AkinoriHashimoto 0:058192932ad2 33 char tmp[1];
AkinoriHashimoto 0:058192932ad2 34 tmp[0]= '0'+ digitNum;
AkinoriHashimoto 0:058192932ad2 35 format += tmp[0];
AkinoriHashimoto 0:058192932ad2 36 format += ".";
AkinoriHashimoto 0:058192932ad2 37 format += tmp[0]; // +でつなげると、charの和(オーバーフロー)起こす。
AkinoriHashimoto 0:058192932ad2 38 }
AkinoriHashimoto 0:058192932ad2 39
AkinoriHashimoto 0:058192932ad2 40 if(base== 8)
AkinoriHashimoto 0:058192932ad2 41 format += "o";
AkinoriHashimoto 0:058192932ad2 42 if(base==10)
AkinoriHashimoto 0:058192932ad2 43 format += "d";
AkinoriHashimoto 0:058192932ad2 44 if(base==16)
AkinoriHashimoto 0:058192932ad2 45 format += "x";
AkinoriHashimoto 0:058192932ad2 46 // log_BTSD(format);
AkinoriHashimoto 0:058192932ad2 47
AkinoriHashimoto 0:058192932ad2 48 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 0:058192932ad2 49 string tmpStr= tmpChr;
AkinoriHashimoto 0:058192932ad2 50 return tmpStr;
AkinoriHashimoto 0:058192932ad2 51 }
AkinoriHashimoto 0:058192932ad2 52
AkinoriHashimoto 0:058192932ad2 53 bool strCompare(string trg, string cmp, int idx)
AkinoriHashimoto 0:058192932ad2 54 {
AkinoriHashimoto 0:058192932ad2 55 int id= trg.find(cmp, idx);
AkinoriHashimoto 0:058192932ad2 56 if(id == string::npos)
AkinoriHashimoto 0:058192932ad2 57 return false;
AkinoriHashimoto 0:058192932ad2 58 return id == idx;
AkinoriHashimoto 0:058192932ad2 59 }