Utility to convert hex formatted ascii to binary system types.
Dependents: Nucleo_read_hyperterminale
Helper for parsing ASCII communications
Example
#include "mbed.h" #include "atoh.h" int main() { uint64_t result = atoh <uint64_t> ("0123456789abcdef" ); uint32_t lo = result & 0x00000000ffffffff; uint32_t hi = (result >> 32); printf( "0x%08X%08X\n", hi, lo ); printf( "0x%08X\n", atoh <uint32_t> ( "12345678" ) ); printf( "0x%04X\n", atoh <uint16_t> ( "1234" ) ); printf( "0x%02X\n", atoh <uint8_t> ( "12" ) ); }
Revision 0:fbac423fa3d4, committed 2013-03-07
- Comitter:
- sam_grove
- Date:
- Thu Mar 07 21:38:29 2013 +0000
- Child:
- 1:c590c304b2fa
- Commit message:
- Cleaned up documentation and updated example
Changed in this revision
atoh.cpp | Show annotated file Show diff for this revision Revisions of this file |
atoh.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/atoh.cpp Thu Mar 07 21:38:29 2013 +0000 @@ -0,0 +1,71 @@ +/** + * @file atoh.cpp + * @brief Convert an ASCII hex string to hexadecimal - seems like the + * std library should include this... maybe it does. Just couldn't find it + * @author sam grove + * @version 1.0 + * + * Copyright (c) 2013 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "atoh.h" + +template<typename T> +T atoh( char const *string ) +{ + T value = 0; + char ch = 0; + uint8_t digit = 0, bail = sizeof(T) << 1; + + // loop until the string has ended + while ( ( ch = *(string++) ) != 0 ) + { + // service digits 0 - 9 + if ( ( ch >= '0' ) && ( ch <= '9' ) ) + { + digit = ch - '0'; + } + // and then lowercase a - f + else if ( ( ch >= 'a' ) && ( ch <= 'f' ) ) + { + digit = ch - 'a' + 10; + } + // and uppercase A - F + else if ( ( ch >= 'A' ) && ( ch <= 'F' ) ) + { + digit = ch - 'A' + 10; + } + // stopping where we are if an inapproprate value is found + else + { + break; + } + // if the return is 8, 16, or 32 bits - only parse that amount + if ( bail-- == 0 ) + { + break; + } + // and build the value now, preparing for the next pass + value = (value << 4) + digit; + } + + return value; +} + +template uint8_t atoh <uint8_t> (const char* string ); +template uint16_t atoh <uint16_t>(const char* string ); +template uint32_t atoh <uint32_t>(const char* string ); +template uint64_t atoh <uint64_t>(const char* string ); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/atoh.h Thu Mar 07 21:38:29 2013 +0000 @@ -0,0 +1,57 @@ +/** + * @file atoh.h + * @brief Convert an ASCII hex string to hexadecimal - seems like the + * std library should include this... maybe it does. Just couldn't find it + * @author sam grove + * @version 1.0 + * + * Copyright (c) 2013 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ATOH_H +#define ATOH_H + +#include <stdint.h> + +/** Convert a ASCII string to it's binary equivenent + * + * Example: + * @code + * #include "mbed.h" + * #include "atoh.h" + * + * int main() + * { + * uint64_t result = atoh <uint64_t> ("0123456789abcdef" ); + * uint32_t lo = result & 0x00000000ffffffff; + * uint32_t hi = (result >> 32); + * printf( "0x%08X%08X\n", hi, lo ); + * printf( "0x%08X\n", atoh <uint32_t> ( "12345678" ) ); + * printf( "0x%04X\n", atoh <uint16_t> ( "1234" ) ); + * printf( "0x%02X\n", atoh <uint8_t> ( "12" ) ); + * } + * @endcode + */ + +/** A templated method for ascii to hex conversions. Supported types are: + * uint8_t, uint16_t, uint32_t and uint64_t + * @param string - An ascii string of hex digits + * @returns The binary equivelant of the string + */ +template<typename T> +T atoh( char const *string ); + +#endif +