📄 hex.c
字号:
/* ---------------------------------------------------------------------------- CFL - A C Foundation Library Copyright (C) 1994-2003 Mark A Lindner This file is part of CFL. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ---------------------------------------------------------------------------- $Log: hex.c,v $ Revision 1.1 2003/03/03 08:20:07 markl Initial checkin (clean compile on Solaris & OS X) ----------------------------------------------------------------------------*//* Feature test switches */#include "config.h"/* System headers */#include <ctype.h>/* Local headers */#include "cfl/defs.h"#include "cfl/util.h"/* Functions */char C_hex_tonibble(int v) { if(v < 0 || v > 15) return(NUL); return(v + ((v < 10) ? '0' : ('A' - 10))); }/* */int C_hex_fromnibble(char c) { if(!C_hex_isdigit(c)) return(-1); c = toupper(c); return(c - ((c < 'A') ? '0' : ('A' - 10))); }/* */c_bool_t C_hex_tobyte(char *s, int v) { div_t d; if(v < 0 || v > 255 || !s) return(FALSE); d = div(v, 16); s[0] = C_hex_tonibble(d.quot); s[1] = C_hex_tonibble(d.rem); s[2] = NUL; return(TRUE); }/* */int C_hex_frombyte(char * const s) { int hi, lo; if(!s) return(-1); if((hi = C_hex_fromnibble(s[0])) < 0) return(-1); if((lo = C_hex_fromnibble(s[1])) < 0) return(-1); return(16 * hi + lo); }/* */c_bool_t C_hex_encode(char * const data, size_t len, char *s) { char *p, *q; int i; if(!data || !len || !s) return(FALSE); for(p = data, i = len, q = s; i--; p++, q += 2) C_hex_tobyte(q, (int)*p); *q = NUL; return(TRUE); }/* */c_bool_t C_hex_decode(char * const s, size_t len, char *data) { char *p, *q; int i; if(!s || !len || (len % 2) || !data) return(FALSE); for(q = s, i = len, p = data; i--; q += 2, p++) *p = (char)C_hex_frombyte(q); return(TRUE); }/* end of source file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -