hex_bytes.c
Go to the documentation of this file.
00001 
00009 /*
00010  * Copyright (c) 2009 ThingMagic, Inc.
00011  *
00012  * Permission is hereby granted, free of charge, to any person obtaining a copy
00013  * of this software and associated documentation files (the "Software"), to deal
00014  * in the Software without restriction, including without limitation the rights
00015  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00016  * copies of the Software, and to permit persons to whom the Software is
00017  * furnished to do so, subject to the following conditions:
00018  *
00019  * The above copyright notice and this permission notice shall be included in
00020  * all copies or substantial portions of the Software.
00021  *
00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00025  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00028  * THE SOFTWARE.
00029  */
00030 
00031 #include <stdlib.h>
00032 
00033 #include "tm_reader.h"
00034 #include "serial_reader_imp.h"
00035 
00036 static char hexchars[] = "0123456789ABCDEF";
00037 
00038 
00039 TMR_Status
00040 TMR_hexToBytes(const char *hex, uint8_t *bytes, uint32_t size,
00041                uint32_t *convertLen)
00042 {
00043   char c;
00044   int i, len;
00045   uint8_t val[2];
00046 
00047   if (hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X'))
00048   {
00049     hex += 2;
00050   }
00051 
00052   len = 0;
00053   while (size > 0 && *hex != '\0')
00054   {
00055     for (i = 0; i < 2; i++)
00056     {
00057       c = *hex++;
00058       if (c >= '0' && c <= '9')
00059         val[i] = c - '0';
00060       else if (c >= 'a' && c <= 'f')
00061         val[i] = 10 + c - 'a';
00062       else if (c >= 'A' && c <= 'F')
00063         val[i] = 10 + c - 'A';
00064       else
00065         return TMR_ERROR_INVALID;
00066     }
00067     *bytes = (val[0] << 4) | val[1];
00068 
00069     len++;
00070     bytes++;
00071     size--;
00072   }
00073 
00074   if (convertLen != NULL)
00075   {
00076     *convertLen = len;
00077   }
00078 
00079   return TMR_SUCCESS;
00080 }
00081 
00082 void
00083 TMR_bytesToHex(const uint8_t *bytes, uint32_t size, char *hex)
00084 {
00085 
00086   while (size--)
00087   {
00088     *hex++ = hexchars[*bytes >> 4];
00089     *hex++ = hexchars[*bytes & 15];
00090     bytes++;
00091   }
00092   *hex = '\0';
00093 }
00094 
00102 void
00103 TMR_hexDottedQuad(const uint8_t bytes[4], char buf[12])
00104 {
00105   int i;
00106 
00107   for (i = 0; i < 4 ; i++)
00108   {
00109     *buf++ = hexchars[*bytes >> 4];
00110     *buf++ = hexchars[*bytes & 15];
00111     *buf++ = '.';
00112     bytes++;
00113   }
00114   *--buf = '\0';
00115 }
00116 
00124 TMR_Status
00125 TMR_hexDottedQuadToUint32(const char bytes[12], uint32_t *result)
00126 {
00127   TMR_Status retVal;
00128   uint32_t tmpResult;
00129   uint8_t byteVal;
00130   int i,j;
00131 
00132   tmpResult=0;
00133   for (i = 0, j = 0; i < 12 ; i+=3,++j)
00134   {
00135     retVal = TMR_hexToBytes(&bytes[i], &byteVal, 1, NULL);
00136     if (TMR_SUCCESS == retVal)
00137     {
00138       tmpResult |= (byteVal << (8 * (3-j)));
00139     }
00140     else
00141     {
00142       return retVal;
00143     }
00144   }
00145 
00146   if (NULL != result)
00147   {
00148     *result = tmpResult;
00149   }
00150   return retVal;
00151 }


thingmagic_rfid
Author(s): Brian Bingham
autogenerated on Thu May 16 2019 03:01:23