60 lines
1.7 KiB
C
60 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* utils/ascii.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/10/13 07:33:17 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include <libp7/internals.h>
|
|
#include <ctype.h>
|
|
|
|
/**
|
|
* p7_putascii:
|
|
* Put a number in ASCII-hex, in a n-dimensionned field.
|
|
*
|
|
* @arg p pointer where to put ASCII number
|
|
* @arg i ASCII number
|
|
* @arg n the size of the field
|
|
*/
|
|
|
|
void p7_putascii(unsigned char *p, p7uint_t i, int n)
|
|
{
|
|
/* goto end of the field */
|
|
p += (n - 1);
|
|
/* for each digit */
|
|
while (n--) {
|
|
/* get end digit from this point */
|
|
int j = i % 16;
|
|
/* put it in ASCII-hex */
|
|
*p-- = j >= 10 ? j - 10 + 'A' : j + '0';
|
|
/* then go to digit that's left */
|
|
i /= 16;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* p7_getascii:
|
|
* Gets a number in ASCII-hex, in a n-dimensionned field.
|
|
*
|
|
* @arg p pointer where the ASCII number is
|
|
* @arg n the size of the field
|
|
* @return the number
|
|
*/
|
|
|
|
p7uint_t p7_getascii(const unsigned char *p, int n)
|
|
{
|
|
p7uint_t i = 0;
|
|
|
|
/* for each digit */
|
|
while (n--) {
|
|
/* get digit from there */
|
|
p7uint_t j = *p++; j = isdigit(j) ? j - '0' : j + 10 - 'A';
|
|
/* then add it to i */
|
|
i = i * 16 + j;
|
|
}
|
|
/* return final number */
|
|
return (i);
|
|
}
|