String library.
Dependents: CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more
Revision 7:7bc89a64bfbd, committed 2015-10-20
- Comitter:
- AkinoriHashimoto
- Date:
- Tue Oct 20 01:23:22 2015 +0000
- Parent:
- 6:4447094b7e51
- Commit message:
- support 64bit
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 |
diff -r 4447094b7e51 -r 7bc89a64bfbd StrLib.cpp --- a/StrLib.cpp Fri Oct 09 05:20:44 2015 +0000 +++ b/StrLib.cpp Tue Oct 20 01:23:22 2015 +0000 @@ -1,49 +1,77 @@ #include "StrLib.h" -long A2I(string str, int base) // Error; rtn '-1' +long long A2I(string str, unsigned int base) // Error; rtn '-1' { - long ans= 0, tmp= 0; + long long ans= 0; // 64bit + int tmp= 0; // 32bit 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; + unsigned int chr; + for(int i= 0; i < leng; i++) { + chr= str[leng-1-i]; + if('0'<=chr && '9'>=chr) + tmp= chr-'0'; + else if('A'<=chr && 'F'>=chr) + tmp= chr-'A'+0x0A; // ex. C=12= 2(C-A)+ 10(0x0A) + else if('a'<=chr && 'f'>=chr) + tmp= chr-'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); + + ans+= (long long)((double)tmp* pow((double)base,(double)i)); } return ans; } -string I2A(int num, int base, int digitNum) +string I2A(int num, unsigned int base, unsigned int digitNum) { char tmpChr[33]; + // "%5.5dなどとしたい string format= "%"; // Digit Num. if(digitNum > 0) { // 0以外 - char tmp[1]; - tmp[0]= '0'+ digitNum; - format += tmp[0]; + unsigned int tmp; + tmp= '0'+ digitNum; + format += tmp; format += "."; - format += tmp[0]; // +でつなげると、charの和(オーバーフロー)起こす。 + format += tmp; // +でつなげると、charの和(オーバーフロー)起こす。 } - if(base== 8) + if( base == 8) format += "o"; - if(base==10) + else if(base == 10) format += "d"; - if(base==16) + else if(base == 16) format += "x"; -// log_BTSD(format); + else + return "ERR; base: Oct, Dec, Hex."; + + sprintf(tmpChr, format.c_str(), num); + string tmpStr= tmpChr; + return tmpStr; +} +string I2A(long long num, unsigned int base, unsigned int digitNum) +{ + char tmpChr[33]; + // "%5.5dなどとしたい + string format= "%"; + + // Digit Num. + if(digitNum > 0) { // 0以外 + unsigned int tmp; + tmp= '0'+ digitNum; + format += tmp; + format += "."; + format += tmp; // +でつなげると、charの和(オーバーフロー)起こす。 + } + + if( base == 8) + format += "llo"; + else if(base == 10) + format += "lld"; + else if(base == 16) + format += "llx"; + else + return "ERR; base: Oct, Dec, Hex."; sprintf(tmpChr, format.c_str(), num); string tmpStr= tmpChr; @@ -91,7 +119,7 @@ idx= cmp.find(trg); if(idx == string::npos) return false; - + return true; }
diff -r 4447094b7e51 -r 7bc89a64bfbd StrLib.h --- a/StrLib.h Fri Oct 09 05:20:44 2015 +0000 +++ b/StrLib.h Tue Oct 20 01:23:22 2015 +0000 @@ -1,25 +1,24 @@ #pragma once -#ifndef MBED_STRLIB_H -#define MBED_STRLIB_H - #include "mbed.h" #include <string> -/** String(Ascii) -> long(int) + +/** String(Ascii) -> "long long"(64bit) integer. * * @param string target(str), int Base(8:Oct, 10:Dec, 16:Hex) - * @return long int + * @return long long int. if -1; error. */ -long A2I(string, int); +long long A2I(string str, unsigned int base); /** int -> string * * @param int target(num), int Base(8:Oct, 10:Dec, 16:Hex), int Number of digit * @return string str */ -string I2A(int num, int base= 10, int digitNum= 0); +string I2A(int num, unsigned int base= 10, unsigned int digitNum= 0); +string I2A(long long num, unsigned int base= 10, unsigned int digitNum= 0); /** float -> string * @@ -46,5 +45,4 @@ */ string toAlpanumeric(string str, bool large=false); -#endif // EOF \ No newline at end of file