/************************************************************************ | |
(c) Copyright 2021 by 天翼物联科技有限公司. All rights reserved. | |
**************************************************************************/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include "LPAdutil.h" | |
uint8_t hex2byte(uint8_t *hexstr) | |
{ | |
uint8_t n1,n2; | |
if( hexstr == NULL || hexstr[0] == 0 || hexstr[1] == 0) | |
return -1; | |
n1 = (hexstr[0] <= '9' && hexstr[0] >= '0')? hexstr[0]-'0':hexstr[0]+10-'A'; | |
n2 = (hexstr[1] <= '9' && hexstr[1] >= '0')? hexstr[1]-'0':hexstr[1]+10-'A'; | |
return ((n1<<4)&0xf0) | (n2&0x0f); | |
} | |
int byte2hex(char *hexstr, char byte) | |
{ | |
char n1,n2; | |
if( hexstr == NULL ) | |
return -1; | |
n1 = (byte&0xf0)>>4; | |
n2 = byte&0X0F; | |
*hexstr = (n1 <= 9) ? n1 + '0' : n1 - 10 + 'A'; | |
hexstr++; | |
*hexstr = (n2 <= 9) ? n2 + '0' : n2 - 10 + 'A'; | |
return 0; | |
} | |
uint8_t byte2hexH(char byte) | |
{ | |
char n; | |
n = (byte&0xf0)>>4; | |
return (n <= 9) ? n + '0' : n - 10 + 'A'; | |
} | |
uint8_t byte2hexL(char byte) | |
{ | |
char n; | |
n = byte&0X0F; | |
return (n <= 9) ? n + '0' : n - 10 + 'A'; | |
} |