String library.
Dependents: CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more
Revision 0:058192932ad2, committed 2015-06-11
- Comitter:
- AkinoriHashimoto
- Date:
- Thu Jun 11 01:50:08 2015 +0000
- Child:
- 1:7c89cd414311
- Commit message:
- string library.; I2A, A2I, compare.
Changed in this revision
StrLib.cpp | Show annotated file Show diff for this revision Revisions of this file |
StrLib.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StrLib.cpp Thu Jun 11 01:50:08 2015 +0000 @@ -0,0 +1,59 @@ +#include "StrLib.h" + +long A2I(string str, int base) // Error; rtn '-1' +{ + long ans= 0, tmp= 0; + int leng= str.size(); // String Length; + char chr[1]; + for(int i=0; i<leng; i++) { + // string -> char + strcpy(chr, str.substr((leng-1)-i, 1).c_str()); + + if('0'<=chr[0] && '9'>=chr[0]) + tmp= chr[0]-'0'; + else if('A'<=chr[0] && 'F'>=chr[0]) + tmp= chr[0]-'A'+0x0A; // ex. C=12= 2(C-A)+ 10(0x0A) + else if('a'<=chr[0] && 'f'>=chr[0]) + tmp= chr[0]-'a'+0x0A; + else + return -1; +// UART_BT.printf("%s;%d\n", chr[0], tmp); + ans+= tmp* (int)pow((double)base,(double)i); +// UART_BT.printf("ans%d\n", ans); + } + return ans; +} +string I2A(int num, int base, int digitNum) +{ + char tmpChr[33]; + string format= "%"; + + // Digit Num. + if(digitNum > 0) { // 0以外 + char tmp[1]; + tmp[0]= '0'+ digitNum; + format += tmp[0]; + format += "."; + format += tmp[0]; // +でつなげると、charの和(オーバーフロー)起こす。 + } + + if(base== 8) + format += "o"; + if(base==10) + format += "d"; + if(base==16) + format += "x"; +// log_BTSD(format); + + sprintf(tmpChr, format.c_str(), num); + string tmpStr= tmpChr; + return tmpStr; +} + +bool strCompare(string trg, string cmp, int idx) +{ + int id= trg.find(cmp, idx); + if(id == string::npos) + return false; + return id == idx; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/StrLib.h Thu Jun 11 01:50:08 2015 +0000 @@ -0,0 +1,23 @@ +#pragma once + +#include "mbed.h" +#include <string> +// class化しないとダメなんだけど。。。 + +/** String -> long + * @param string target(str), int Base(8Oct, 10Dec, 16Hex) + * @return long int +*/ +long A2I(string, int); // Ascii → Intへ変換する関数。 + +/** int -> string + * @param int target(num), int Base(8Oct, 10Dec, 16Hex), int Number of digit + * @return string str +*/ +string I2A(int num, int base= 10, int digitNum= 0); // 標準引数 + +/** string Compare + * @param string target, string cmp, int index in target + * @return bool +*/ +bool strCompare(string trg, string cmp, int idx); // trg内にcmpが存在する位置がidxか判定。