String library.

Dependents:   CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more

Committer:
AkinoriHashimoto
Date:
Wed Sep 02 05:42:13 2015 +0000
Revision:
5:9afdff23f890
Parent:
4:17e03f0747d9
Child:
7:7bc89a64bfbd
F2A; fill 0.

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 5:9afdff23f890 53 string F2A(float num, int fieldWidth, int decimalPlaces, bool fill0)
AkinoriHashimoto 2:14f3ff21096e 54 {
AkinoriHashimoto 2:14f3ff21096e 55 if(!(0<=fieldWidth && fieldWidth<30))
AkinoriHashimoto 2:14f3ff21096e 56 return "ERR; fieldWidth.";
AkinoriHashimoto 2:14f3ff21096e 57 if(!(0<=decimalPlaces && decimalPlaces<30) )
AkinoriHashimoto 2:14f3ff21096e 58 return "ERR; decimalPlaces.";
AkinoriHashimoto 2:14f3ff21096e 59 if(fieldWidth < decimalPlaces+2)
AkinoriHashimoto 2:14f3ff21096e 60 return "ERR; fieldWidth < decimalPlaces+2";
AkinoriHashimoto 2:14f3ff21096e 61
AkinoriHashimoto 2:14f3ff21096e 62 char tmpChr[33];
AkinoriHashimoto 5:9afdff23f890 63 string format= "%";
AkinoriHashimoto 5:9afdff23f890 64 if(fill0)
AkinoriHashimoto 5:9afdff23f890 65 format += "0";
AkinoriHashimoto 5:9afdff23f890 66 format += I2A(fieldWidth)+ "."+ I2A(decimalPlaces)+ "f";
AkinoriHashimoto 2:14f3ff21096e 67 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 2:14f3ff21096e 68 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 69 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 70 }
AkinoriHashimoto 2:14f3ff21096e 71 string F2A(float num)
AkinoriHashimoto 2:14f3ff21096e 72 {
AkinoriHashimoto 2:14f3ff21096e 73 char tmpChr[33];
AkinoriHashimoto 2:14f3ff21096e 74 sprintf(tmpChr, "%f", num);
AkinoriHashimoto 2:14f3ff21096e 75 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 76 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 77 }
AkinoriHashimoto 2:14f3ff21096e 78
AkinoriHashimoto 0:058192932ad2 79 bool strCompare(string trg, string cmp, int idx)
AkinoriHashimoto 0:058192932ad2 80 {
AkinoriHashimoto 0:058192932ad2 81 int id= trg.find(cmp, idx);
AkinoriHashimoto 0:058192932ad2 82 if(id == string::npos)
AkinoriHashimoto 0:058192932ad2 83 return false;
AkinoriHashimoto 0:058192932ad2 84 return id == idx;
AkinoriHashimoto 1:7c89cd414311 85 }
AkinoriHashimoto 4:17e03f0747d9 86 bool strCompareComplete(string trg, string cmp)
AkinoriHashimoto 4:17e03f0747d9 87 {
AkinoriHashimoto 4:17e03f0747d9 88 int idx= trg.find(cmp);
AkinoriHashimoto 4:17e03f0747d9 89 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 90 return false;
AkinoriHashimoto 4:17e03f0747d9 91 idx= cmp.find(trg);
AkinoriHashimoto 4:17e03f0747d9 92 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 93 return false;
AkinoriHashimoto 4:17e03f0747d9 94
AkinoriHashimoto 4:17e03f0747d9 95 return true;
AkinoriHashimoto 4:17e03f0747d9 96 }
AkinoriHashimoto 1:7c89cd414311 97
AkinoriHashimoto 1:7c89cd414311 98 string toAlpanumeric(string str, bool toLarge)
AkinoriHashimoto 1:7c89cd414311 99 {
AkinoriHashimoto 1:7c89cd414311 100 string ans= "";
AkinoriHashimoto 1:7c89cd414311 101 bool num, small, large;
AkinoriHashimoto 1:7c89cd414311 102 for (int i= 0; i < str.size(); i++) {
AkinoriHashimoto 1:7c89cd414311 103 num= small= large= false;
AkinoriHashimoto 1:7c89cd414311 104 if('0' <= str[i] && str[i] <= '9')
AkinoriHashimoto 1:7c89cd414311 105 num= true;
AkinoriHashimoto 1:7c89cd414311 106 else if('A' <= str[i] && str[i] <= 'Z')
AkinoriHashimoto 1:7c89cd414311 107 large= true;
AkinoriHashimoto 1:7c89cd414311 108 else if('a' <= str[i] && str[i] <= 'z')
AkinoriHashimoto 1:7c89cd414311 109 small= true;
AkinoriHashimoto 2:14f3ff21096e 110
AkinoriHashimoto 1:7c89cd414311 111 if(toLarge && small)
AkinoriHashimoto 1:7c89cd414311 112 str[i] -= 0x20; // small -> large
AkinoriHashimoto 1:7c89cd414311 113 if(num || large || small)
AkinoriHashimoto 1:7c89cd414311 114 ans += str[i];
AkinoriHashimoto 1:7c89cd414311 115 }
AkinoriHashimoto 2:14f3ff21096e 116
AkinoriHashimoto 1:7c89cd414311 117 return ans;
AkinoriHashimoto 0:058192932ad2 118 }