home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
cprog
/
actlib12.zip
/
STRINGS.ZIP
/
STRTOHEX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-02-25
|
969b
|
38 lines
/* Copyright (C) 1993 Marc Stern (internet: stern@mble.philips.be) */
#include "strings.h"
#include <stdio.h>
/***
* Function : strtohex
*
* Description : convert a string containing an hexadecimal
* representation into an hexadecimal flow of bytes.
*
* Parameters : char *string in/out
*
* Return : length of hexadecimal flow of bytes.
* -1 on error
*
* OS/Compiler : All
***/
int strtohex( char *string )
{ int len = 0;
char *ptr = string, buffer[2];
/* if string length is even, first char is a full number */
if ( strlen(string) % 2 ) { if ( sscanf(ptr, "%01x", buffer) != 1 ) return -1;
*ptr++ = *buffer;
len = 1;
}
while ( *ptr )
{ if ( sscanf( ptr, "%02x", string + len++) != 1 ) return -1;
ptr += 2;
}
return len;
}