String library.

Dependents:   CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more

Committer:
AkinoriHashimoto
Date:
Mon Jun 29 01:51:12 2015 +0000
Revision:
1:7c89cd414311
Parent:
0:058192932ad2
Child:
2:14f3ff21096e
my string library.

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 1:7c89cd414311 59 }
AkinoriHashimoto 1:7c89cd414311 60
AkinoriHashimoto 1:7c89cd414311 61 string toAlpanumeric(string str, bool toLarge)
AkinoriHashimoto 1:7c89cd414311 62 {
AkinoriHashimoto 1:7c89cd414311 63 string ans= "";
AkinoriHashimoto 1:7c89cd414311 64 bool num, small, large;
AkinoriHashimoto 1:7c89cd414311 65 for (int i= 0; i < str.size(); i++) {
AkinoriHashimoto 1:7c89cd414311 66 num= small= large= false;
AkinoriHashimoto 1:7c89cd414311 67 if('0' <= str[i] && str[i] <= '9')
AkinoriHashimoto 1:7c89cd414311 68 num= true;
AkinoriHashimoto 1:7c89cd414311 69 else if('A' <= str[i] && str[i] <= 'Z')
AkinoriHashimoto 1:7c89cd414311 70 large= true;
AkinoriHashimoto 1:7c89cd414311 71 else if('a' <= str[i] && str[i] <= 'z')
AkinoriHashimoto 1:7c89cd414311 72 small= true;
AkinoriHashimoto 1:7c89cd414311 73
AkinoriHashimoto 1:7c89cd414311 74 if(toLarge && small)
AkinoriHashimoto 1:7c89cd414311 75 str[i] -= 0x20; // small -> large
AkinoriHashimoto 1:7c89cd414311 76 if(num || large || small)
AkinoriHashimoto 1:7c89cd414311 77 ans += str[i];
AkinoriHashimoto 1:7c89cd414311 78 }
AkinoriHashimoto 1:7c89cd414311 79
AkinoriHashimoto 1:7c89cd414311 80 return ans;
AkinoriHashimoto 0:058192932ad2 81 }