String library.

Dependents:   CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more

Committer:
AkinoriHashimoto
Date:
Tue Oct 20 01:23:22 2015 +0000
Revision:
7:7bc89a64bfbd
Parent:
5:9afdff23f890
support 64bit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:058192932ad2 1 #include "StrLib.h"
AkinoriHashimoto 0:058192932ad2 2
AkinoriHashimoto 7:7bc89a64bfbd 3 long long A2I(string str, unsigned int base) // Error; rtn '-1'
AkinoriHashimoto 0:058192932ad2 4 {
AkinoriHashimoto 7:7bc89a64bfbd 5 long long ans= 0; // 64bit
AkinoriHashimoto 7:7bc89a64bfbd 6 int tmp= 0; // 32bit
AkinoriHashimoto 0:058192932ad2 7 int leng= str.size(); // String Length;
AkinoriHashimoto 7:7bc89a64bfbd 8 unsigned int chr;
AkinoriHashimoto 7:7bc89a64bfbd 9 for(int i= 0; i < leng; i++) {
AkinoriHashimoto 7:7bc89a64bfbd 10 chr= str[leng-1-i];
AkinoriHashimoto 7:7bc89a64bfbd 11 if('0'<=chr && '9'>=chr)
AkinoriHashimoto 7:7bc89a64bfbd 12 tmp= chr-'0';
AkinoriHashimoto 7:7bc89a64bfbd 13 else if('A'<=chr && 'F'>=chr)
AkinoriHashimoto 7:7bc89a64bfbd 14 tmp= chr-'A'+0x0A; // ex. C=12= 2(C-A)+ 10(0x0A)
AkinoriHashimoto 7:7bc89a64bfbd 15 else if('a'<=chr && 'f'>=chr)
AkinoriHashimoto 7:7bc89a64bfbd 16 tmp= chr-'a'+0x0A;
AkinoriHashimoto 0:058192932ad2 17 else
AkinoriHashimoto 0:058192932ad2 18 return -1;
AkinoriHashimoto 7:7bc89a64bfbd 19
AkinoriHashimoto 7:7bc89a64bfbd 20 ans+= (long long)((double)tmp* pow((double)base,(double)i));
AkinoriHashimoto 0:058192932ad2 21 }
AkinoriHashimoto 0:058192932ad2 22 return ans;
AkinoriHashimoto 0:058192932ad2 23 }
AkinoriHashimoto 7:7bc89a64bfbd 24 string I2A(int num, unsigned int base, unsigned int digitNum)
AkinoriHashimoto 0:058192932ad2 25 {
AkinoriHashimoto 0:058192932ad2 26 char tmpChr[33];
AkinoriHashimoto 7:7bc89a64bfbd 27 // "%5.5dなどとしたい
AkinoriHashimoto 0:058192932ad2 28 string format= "%";
AkinoriHashimoto 0:058192932ad2 29
AkinoriHashimoto 0:058192932ad2 30 // Digit Num.
AkinoriHashimoto 0:058192932ad2 31 if(digitNum > 0) { // 0以外
AkinoriHashimoto 7:7bc89a64bfbd 32 unsigned int tmp;
AkinoriHashimoto 7:7bc89a64bfbd 33 tmp= '0'+ digitNum;
AkinoriHashimoto 7:7bc89a64bfbd 34 format += tmp;
AkinoriHashimoto 0:058192932ad2 35 format += ".";
AkinoriHashimoto 7:7bc89a64bfbd 36 format += tmp; // +でつなげると、charの和(オーバーフロー)起こす。
AkinoriHashimoto 0:058192932ad2 37 }
AkinoriHashimoto 0:058192932ad2 38
AkinoriHashimoto 7:7bc89a64bfbd 39 if( base == 8)
AkinoriHashimoto 0:058192932ad2 40 format += "o";
AkinoriHashimoto 7:7bc89a64bfbd 41 else if(base == 10)
AkinoriHashimoto 0:058192932ad2 42 format += "d";
AkinoriHashimoto 7:7bc89a64bfbd 43 else if(base == 16)
AkinoriHashimoto 0:058192932ad2 44 format += "x";
AkinoriHashimoto 7:7bc89a64bfbd 45 else
AkinoriHashimoto 7:7bc89a64bfbd 46 return "ERR; base: Oct, Dec, Hex.";
AkinoriHashimoto 7:7bc89a64bfbd 47
AkinoriHashimoto 7:7bc89a64bfbd 48 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 7:7bc89a64bfbd 49 string tmpStr= tmpChr;
AkinoriHashimoto 7:7bc89a64bfbd 50 return tmpStr;
AkinoriHashimoto 7:7bc89a64bfbd 51 }
AkinoriHashimoto 7:7bc89a64bfbd 52 string I2A(long long num, unsigned int base, unsigned int digitNum)
AkinoriHashimoto 7:7bc89a64bfbd 53 {
AkinoriHashimoto 7:7bc89a64bfbd 54 char tmpChr[33];
AkinoriHashimoto 7:7bc89a64bfbd 55 // "%5.5dなどとしたい
AkinoriHashimoto 7:7bc89a64bfbd 56 string format= "%";
AkinoriHashimoto 7:7bc89a64bfbd 57
AkinoriHashimoto 7:7bc89a64bfbd 58 // Digit Num.
AkinoriHashimoto 7:7bc89a64bfbd 59 if(digitNum > 0) { // 0以外
AkinoriHashimoto 7:7bc89a64bfbd 60 unsigned int tmp;
AkinoriHashimoto 7:7bc89a64bfbd 61 tmp= '0'+ digitNum;
AkinoriHashimoto 7:7bc89a64bfbd 62 format += tmp;
AkinoriHashimoto 7:7bc89a64bfbd 63 format += ".";
AkinoriHashimoto 7:7bc89a64bfbd 64 format += tmp; // +でつなげると、charの和(オーバーフロー)起こす。
AkinoriHashimoto 7:7bc89a64bfbd 65 }
AkinoriHashimoto 7:7bc89a64bfbd 66
AkinoriHashimoto 7:7bc89a64bfbd 67 if( base == 8)
AkinoriHashimoto 7:7bc89a64bfbd 68 format += "llo";
AkinoriHashimoto 7:7bc89a64bfbd 69 else if(base == 10)
AkinoriHashimoto 7:7bc89a64bfbd 70 format += "lld";
AkinoriHashimoto 7:7bc89a64bfbd 71 else if(base == 16)
AkinoriHashimoto 7:7bc89a64bfbd 72 format += "llx";
AkinoriHashimoto 7:7bc89a64bfbd 73 else
AkinoriHashimoto 7:7bc89a64bfbd 74 return "ERR; base: Oct, Dec, Hex.";
AkinoriHashimoto 0:058192932ad2 75
AkinoriHashimoto 0:058192932ad2 76 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 0:058192932ad2 77 string tmpStr= tmpChr;
AkinoriHashimoto 0:058192932ad2 78 return tmpStr;
AkinoriHashimoto 0:058192932ad2 79 }
AkinoriHashimoto 0:058192932ad2 80
AkinoriHashimoto 5:9afdff23f890 81 string F2A(float num, int fieldWidth, int decimalPlaces, bool fill0)
AkinoriHashimoto 2:14f3ff21096e 82 {
AkinoriHashimoto 2:14f3ff21096e 83 if(!(0<=fieldWidth && fieldWidth<30))
AkinoriHashimoto 2:14f3ff21096e 84 return "ERR; fieldWidth.";
AkinoriHashimoto 2:14f3ff21096e 85 if(!(0<=decimalPlaces && decimalPlaces<30) )
AkinoriHashimoto 2:14f3ff21096e 86 return "ERR; decimalPlaces.";
AkinoriHashimoto 2:14f3ff21096e 87 if(fieldWidth < decimalPlaces+2)
AkinoriHashimoto 2:14f3ff21096e 88 return "ERR; fieldWidth < decimalPlaces+2";
AkinoriHashimoto 2:14f3ff21096e 89
AkinoriHashimoto 2:14f3ff21096e 90 char tmpChr[33];
AkinoriHashimoto 5:9afdff23f890 91 string format= "%";
AkinoriHashimoto 5:9afdff23f890 92 if(fill0)
AkinoriHashimoto 5:9afdff23f890 93 format += "0";
AkinoriHashimoto 5:9afdff23f890 94 format += I2A(fieldWidth)+ "."+ I2A(decimalPlaces)+ "f";
AkinoriHashimoto 2:14f3ff21096e 95 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 2:14f3ff21096e 96 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 97 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 98 }
AkinoriHashimoto 2:14f3ff21096e 99 string F2A(float num)
AkinoriHashimoto 2:14f3ff21096e 100 {
AkinoriHashimoto 2:14f3ff21096e 101 char tmpChr[33];
AkinoriHashimoto 2:14f3ff21096e 102 sprintf(tmpChr, "%f", num);
AkinoriHashimoto 2:14f3ff21096e 103 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 104 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 105 }
AkinoriHashimoto 2:14f3ff21096e 106
AkinoriHashimoto 0:058192932ad2 107 bool strCompare(string trg, string cmp, int idx)
AkinoriHashimoto 0:058192932ad2 108 {
AkinoriHashimoto 0:058192932ad2 109 int id= trg.find(cmp, idx);
AkinoriHashimoto 0:058192932ad2 110 if(id == string::npos)
AkinoriHashimoto 0:058192932ad2 111 return false;
AkinoriHashimoto 0:058192932ad2 112 return id == idx;
AkinoriHashimoto 1:7c89cd414311 113 }
AkinoriHashimoto 4:17e03f0747d9 114 bool strCompareComplete(string trg, string cmp)
AkinoriHashimoto 4:17e03f0747d9 115 {
AkinoriHashimoto 4:17e03f0747d9 116 int idx= trg.find(cmp);
AkinoriHashimoto 4:17e03f0747d9 117 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 118 return false;
AkinoriHashimoto 4:17e03f0747d9 119 idx= cmp.find(trg);
AkinoriHashimoto 4:17e03f0747d9 120 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 121 return false;
AkinoriHashimoto 7:7bc89a64bfbd 122
AkinoriHashimoto 4:17e03f0747d9 123 return true;
AkinoriHashimoto 4:17e03f0747d9 124 }
AkinoriHashimoto 1:7c89cd414311 125
AkinoriHashimoto 1:7c89cd414311 126 string toAlpanumeric(string str, bool toLarge)
AkinoriHashimoto 1:7c89cd414311 127 {
AkinoriHashimoto 1:7c89cd414311 128 string ans= "";
AkinoriHashimoto 1:7c89cd414311 129 bool num, small, large;
AkinoriHashimoto 1:7c89cd414311 130 for (int i= 0; i < str.size(); i++) {
AkinoriHashimoto 1:7c89cd414311 131 num= small= large= false;
AkinoriHashimoto 1:7c89cd414311 132 if('0' <= str[i] && str[i] <= '9')
AkinoriHashimoto 1:7c89cd414311 133 num= true;
AkinoriHashimoto 1:7c89cd414311 134 else if('A' <= str[i] && str[i] <= 'Z')
AkinoriHashimoto 1:7c89cd414311 135 large= true;
AkinoriHashimoto 1:7c89cd414311 136 else if('a' <= str[i] && str[i] <= 'z')
AkinoriHashimoto 1:7c89cd414311 137 small= true;
AkinoriHashimoto 2:14f3ff21096e 138
AkinoriHashimoto 1:7c89cd414311 139 if(toLarge && small)
AkinoriHashimoto 1:7c89cd414311 140 str[i] -= 0x20; // small -> large
AkinoriHashimoto 1:7c89cd414311 141 if(num || large || small)
AkinoriHashimoto 1:7c89cd414311 142 ans += str[i];
AkinoriHashimoto 1:7c89cd414311 143 }
AkinoriHashimoto 2:14f3ff21096e 144
AkinoriHashimoto 1:7c89cd414311 145 return ans;
AkinoriHashimoto 0:058192932ad2 146 }