Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: strings.cpp
- Revision:
- 1:d5452e398b76
- Parent:
- 0:e0b964252a05
--- a/strings.cpp Wed May 19 12:39:18 2010 +0000
+++ b/strings.cpp Tue Sep 14 21:02:04 2010 +0000
@@ -1,6 +1,7 @@
/*******************************************************************************
-Strings.cpp - By Sophie Dexter, April 2010
+strings.cpp
+(c) 2010 by Sophie Dexter
This C++ module provides functions for working with 'strings' of ascii 'char's
@@ -15,6 +16,7 @@
accept liability for any damage arising from its use.
*******************************************************************************/
+
#include "strings.h"
// copies a string, s2, (array of chars) to another string, s1.
@@ -81,4 +83,81 @@
s++;
}
return s;
-}
\ No newline at end of file
+}
+
+// StrAddc adds a single char to the end of a string
+
+char *StrAddc (char *s, const char c) {
+ char *s1 = s;
+
+// Find the end of the string 's'
+ while (*s)
+ *s++;
+// add the new character
+ *s++ = c;
+// put the end of string character at its new position
+ *s = '\0';
+ return s1;
+}
+
+//-----------------------------------------------------------------------------
+/**
+ Converts an ASCII character to low nibble of a byte.
+
+ @param rx_byte character to convert
+
+ @return resulting value
+*/
+uint8_t ascii2nibble(char str)
+{
+ return str >= 'a' ? (str - 'a' + 10) & 0x0f :
+ (str >= 'A' ? (str - 'A' + 10) & 0x0f :
+ (str - '0') & 0x0f);
+}
+
+//-----------------------------------------------------------------------------
+/**
+ Converts an ASCII string to integer (checks string contents beforehand).
+
+ @param val destination integer
+ @param str pointer to source string
+ @param length length of source string
+
+ @return succ / fail
+*/
+bool ascii2int(uint32_t* val, const char* str, uint8_t length)
+{
+ // nothing to convert
+ if (!str || length < 1)
+ {
+ *val = 0;
+ return false;
+ }
+
+ // check string contents
+ uint8_t shift;
+ for (shift = 0; shift < length; ++shift)
+ {
+ if (!isxdigit(*(str + shift)))
+ {
+ // not a hex value
+ *val = 0;
+ return false;
+ }
+ }
+
+ // convert string
+ *val = ascii2nibble(*(str++));
+ for (shift = 1; shift < length; ++shift)
+ {
+ *val <<= 4;
+ *val += ascii2nibble(*(str++));
+ }
+ return true;
+}
+
+int isxdigit ( int ch )
+{
+ return (unsigned int)( ch - '0') < 10u ||
+ (unsigned int)((ch | 0x20) - 'a') < 6u;
+}