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" ) );
}
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?

UserRevisionLine numberNew contents of line
sam_grove 0:fbac423fa3d4 1 /**
sam_grove 0:fbac423fa3d4 2 * @file atoh.h
sam_grove 1:c590c304b2fa 3 * @brief Convert a hex formatted ASCII hex string to hex. 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 #ifndef ATOH_H
sam_grove 0:fbac423fa3d4 24 #define ATOH_H
sam_grove 0:fbac423fa3d4 25
sam_grove 0:fbac423fa3d4 26 #include <stdint.h>
sam_grove 0:fbac423fa3d4 27
sam_grove 1:c590c304b2fa 28 /** Convert a hex formatted ASCII string to it's binary equivenent
sam_grove 0:fbac423fa3d4 29 *
sam_grove 0:fbac423fa3d4 30 * Example:
sam_grove 0:fbac423fa3d4 31 * @code
sam_grove 0:fbac423fa3d4 32 * #include "mbed.h"
sam_grove 0:fbac423fa3d4 33 * #include "atoh.h"
sam_grove 0:fbac423fa3d4 34 *
sam_grove 0:fbac423fa3d4 35 * int main()
sam_grove 0:fbac423fa3d4 36 * {
sam_grove 0:fbac423fa3d4 37 * uint64_t result = atoh <uint64_t> ("0123456789abcdef" );
sam_grove 0:fbac423fa3d4 38 * uint32_t lo = result & 0x00000000ffffffff;
sam_grove 0:fbac423fa3d4 39 * uint32_t hi = (result >> 32);
sam_grove 0:fbac423fa3d4 40 * printf( "0x%08X%08X\n", hi, lo );
sam_grove 0:fbac423fa3d4 41 * printf( "0x%08X\n", atoh <uint32_t> ( "12345678" ) );
sam_grove 0:fbac423fa3d4 42 * printf( "0x%04X\n", atoh <uint16_t> ( "1234" ) );
sam_grove 0:fbac423fa3d4 43 * printf( "0x%02X\n", atoh <uint8_t> ( "12" ) );
sam_grove 0:fbac423fa3d4 44 * }
sam_grove 0:fbac423fa3d4 45 * @endcode
sam_grove 0:fbac423fa3d4 46 */
sam_grove 0:fbac423fa3d4 47
sam_grove 0:fbac423fa3d4 48 /** A templated method for ascii to hex conversions. Supported types are:
sam_grove 0:fbac423fa3d4 49 * uint8_t, uint16_t, uint32_t and uint64_t
sam_grove 0:fbac423fa3d4 50 * @param string - An ascii string of hex digits
sam_grove 0:fbac423fa3d4 51 * @returns The binary equivelant of the string
sam_grove 0:fbac423fa3d4 52 */
sam_grove 0:fbac423fa3d4 53 template<typename T>
sam_grove 0:fbac423fa3d4 54 T atoh( char const *string );
sam_grove 0:fbac423fa3d4 55
sam_grove 0:fbac423fa3d4 56 #endif
sam_grove 0:fbac423fa3d4 57