Utility to convert hex formatted ascii to binary system types.

Dependents:   Nucleo_read_hyperterminale

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers atoh.h Source File

atoh.h

Go to the documentation of this file.
00001 /**
00002  * @file   atoh.h
00003  * @brief  Convert a hex formatted ASCII hex string to hex. Seems like the 
00004  *  std library should include this... maybe it does. Just couldn't find it
00005  * @author  sam grove
00006  * @version 1.0
00007  *
00008  * Copyright (c) 2013
00009  *
00010  * Licensed under the Apache License, Version 2.0 (the "License");
00011  * you may not use this file except in compliance with the License.
00012  * You may obtain a copy of the License at
00013  *
00014  *     http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  * Unless required by applicable law or agreed to in writing, software
00017  * distributed under the License is distributed on an "AS IS" BASIS,
00018  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  * See the License for the specific language governing permissions and
00020  * limitations under the License.
00021  */
00022  
00023 #ifndef ATOH_H
00024 #define ATOH_H
00025 
00026 #include <stdint.h>
00027 
00028 /** Convert a hex formatted ASCII string to it's binary equivenent
00029  *
00030  * Example:
00031  * @code
00032  *  #include "mbed.h"
00033  *  #include "atoh.h"
00034  * 
00035  *  int main()
00036  *  {
00037  *      uint64_t result = atoh <uint64_t> ("0123456789abcdef" );
00038  *      uint32_t lo = result & 0x00000000ffffffff;
00039  *      uint32_t hi = (result >> 32);
00040  *      printf( "0x%08X%08X\n", hi, lo );
00041  *      printf( "0x%08X\n", atoh <uint32_t> ( "12345678" ) );
00042  *      printf( "0x%04X\n", atoh <uint16_t> ( "1234" ) );
00043  *      printf( "0x%02X\n", atoh <uint8_t> ( "12" ) );
00044  *  }
00045  * @endcode
00046  */
00047 
00048 /** A templated method for ascii to hex conversions. Supported types are:
00049  *  uint8_t, uint16_t, uint32_t and uint64_t
00050  *  @param string - An ascii string of hex digits
00051  *  @returns The binary equivelant of the string
00052  */
00053 template<typename T>
00054 T atoh( char const *string );
00055 
00056 #endif
00057