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.
Dependents: Nucleo_read_hyperterminale
atoh.cpp@1:c590c304b2fa, 2013-03-27 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Mar 27 15:17:57 2013 +0000
- Revision:
- 1:c590c304b2fa
- Parent:
- 0:fbac423fa3d4
Updated some documentation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sam_grove | 0:fbac423fa3d4 | 1 | /** |
| sam_grove | 0:fbac423fa3d4 | 2 | * @file atoh.cpp |
| sam_grove | 0:fbac423fa3d4 | 3 | * @brief Convert an ASCII hex string to hexadecimal - seems like the |
| sam_grove | 0:fbac423fa3d4 | 4 | * std library should include this... maybe it does. Just couldn't find it |
| sam_grove | 0:fbac423fa3d4 | 5 | * @author sam grove |
| sam_grove | 0:fbac423fa3d4 | 6 | * @version 1.0 |
| sam_grove | 0:fbac423fa3d4 | 7 | * |
| sam_grove | 0:fbac423fa3d4 | 8 | * Copyright (c) 2013 |
| sam_grove | 0:fbac423fa3d4 | 9 | * |
| sam_grove | 0:fbac423fa3d4 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| sam_grove | 0:fbac423fa3d4 | 11 | * you may not use this file except in compliance with the License. |
| sam_grove | 0:fbac423fa3d4 | 12 | * You may obtain a copy of the License at |
| sam_grove | 0:fbac423fa3d4 | 13 | * |
| sam_grove | 0:fbac423fa3d4 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| sam_grove | 0:fbac423fa3d4 | 15 | * |
| sam_grove | 0:fbac423fa3d4 | 16 | * Unless required by applicable law or agreed to in writing, software |
| sam_grove | 0:fbac423fa3d4 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| sam_grove | 0:fbac423fa3d4 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| sam_grove | 0:fbac423fa3d4 | 19 | * See the License for the specific language governing permissions and |
| sam_grove | 0:fbac423fa3d4 | 20 | * limitations under the License. |
| sam_grove | 0:fbac423fa3d4 | 21 | */ |
| sam_grove | 0:fbac423fa3d4 | 22 | |
| sam_grove | 0:fbac423fa3d4 | 23 | #include "atoh.h" |
| sam_grove | 0:fbac423fa3d4 | 24 | |
| sam_grove | 0:fbac423fa3d4 | 25 | template<typename T> |
| sam_grove | 0:fbac423fa3d4 | 26 | T atoh( char const *string ) |
| sam_grove | 0:fbac423fa3d4 | 27 | { |
| sam_grove | 0:fbac423fa3d4 | 28 | T value = 0; |
| sam_grove | 0:fbac423fa3d4 | 29 | char ch = 0; |
| sam_grove | 0:fbac423fa3d4 | 30 | uint8_t digit = 0, bail = sizeof(T) << 1; |
| sam_grove | 0:fbac423fa3d4 | 31 | |
| sam_grove | 0:fbac423fa3d4 | 32 | // loop until the string has ended |
| sam_grove | 0:fbac423fa3d4 | 33 | while ( ( ch = *(string++) ) != 0 ) |
| sam_grove | 0:fbac423fa3d4 | 34 | { |
| sam_grove | 0:fbac423fa3d4 | 35 | // service digits 0 - 9 |
| sam_grove | 0:fbac423fa3d4 | 36 | if ( ( ch >= '0' ) && ( ch <= '9' ) ) |
| sam_grove | 0:fbac423fa3d4 | 37 | { |
| sam_grove | 0:fbac423fa3d4 | 38 | digit = ch - '0'; |
| sam_grove | 0:fbac423fa3d4 | 39 | } |
| sam_grove | 0:fbac423fa3d4 | 40 | // and then lowercase a - f |
| sam_grove | 0:fbac423fa3d4 | 41 | else if ( ( ch >= 'a' ) && ( ch <= 'f' ) ) |
| sam_grove | 0:fbac423fa3d4 | 42 | { |
| sam_grove | 0:fbac423fa3d4 | 43 | digit = ch - 'a' + 10; |
| sam_grove | 0:fbac423fa3d4 | 44 | } |
| sam_grove | 0:fbac423fa3d4 | 45 | // and uppercase A - F |
| sam_grove | 0:fbac423fa3d4 | 46 | else if ( ( ch >= 'A' ) && ( ch <= 'F' ) ) |
| sam_grove | 0:fbac423fa3d4 | 47 | { |
| sam_grove | 0:fbac423fa3d4 | 48 | digit = ch - 'A' + 10; |
| sam_grove | 0:fbac423fa3d4 | 49 | } |
| sam_grove | 0:fbac423fa3d4 | 50 | // stopping where we are if an inapproprate value is found |
| sam_grove | 0:fbac423fa3d4 | 51 | else |
| sam_grove | 0:fbac423fa3d4 | 52 | { |
| sam_grove | 0:fbac423fa3d4 | 53 | break; |
| sam_grove | 0:fbac423fa3d4 | 54 | } |
| sam_grove | 0:fbac423fa3d4 | 55 | // if the return is 8, 16, or 32 bits - only parse that amount |
| sam_grove | 0:fbac423fa3d4 | 56 | if ( bail-- == 0 ) |
| sam_grove | 0:fbac423fa3d4 | 57 | { |
| sam_grove | 0:fbac423fa3d4 | 58 | break; |
| sam_grove | 0:fbac423fa3d4 | 59 | } |
| sam_grove | 0:fbac423fa3d4 | 60 | // and build the value now, preparing for the next pass |
| sam_grove | 0:fbac423fa3d4 | 61 | value = (value << 4) + digit; |
| sam_grove | 0:fbac423fa3d4 | 62 | } |
| sam_grove | 0:fbac423fa3d4 | 63 | |
| sam_grove | 0:fbac423fa3d4 | 64 | return value; |
| sam_grove | 0:fbac423fa3d4 | 65 | } |
| sam_grove | 0:fbac423fa3d4 | 66 | |
| sam_grove | 0:fbac423fa3d4 | 67 | template uint8_t atoh <uint8_t> (const char* string ); |
| sam_grove | 0:fbac423fa3d4 | 68 | template uint16_t atoh <uint16_t>(const char* string ); |
| sam_grove | 0:fbac423fa3d4 | 69 | template uint32_t atoh <uint32_t>(const char* string ); |
| sam_grove | 0:fbac423fa3d4 | 70 | template uint64_t atoh <uint64_t>(const char* string ); |
| sam_grove | 0:fbac423fa3d4 | 71 |