Code-Schnipsel
Moderatoren: broesel, Martin Conrad, PatrickThema: hex2ascii
Re: hex2ascii
broesel (webmaster) am 05.08.2010 um 12:36
RFC 1738: Uniform Resource Locators (URL) specification:
Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
Mit den reserved characters meinen die dann wohl :, @, ?, = usw., und mit den reserved purposes dann eben "nicht kodieren, wenn's eine Bedeutung hat".
thx%26bye
Philip
--
The C Programming Quiz
- bitte Fragen einreichen :)
Re: hex2ascii
Patrick (Moderator) am 10.08.2010 um 12:39
#define is_urlencode_special_char(c) (!(isalnum(c) || (strchr("-_ .", c) != NULL)))
/*******************************************************************************
* URL-Enkodiert Kopie eines Strings.
*
* @param const char* String, der URL-Enkodiert werden soll.
* @return char* URL-Enkodierter String oder NULL
*******************************************************************************/
inline
static
char *
urlencode ( const char *decoded )
{
char *encoded = NULL;
char *index = (char *) decoded;
size_t length = 0;
while (index && *index)
{
length += is_urlencode_special_char(*index)
? 3
: 1;
++index;
}
if (0 == length
|| NULL == (encoded=malloc(length+1)))
return NULL;
index = (char *) encoded;
while (decoded && *decoded)
{
if (is_urlencode_special_char(*decoded))
{
*index++ = '%';
*index++ = "0123456789ABCDEF"[(*decoded & 0xF0) >> 4];
*index++ = "0123456789ABCDEF"[(*decoded & 0x0F)];
}
else
{
*index++ = (' ' == *decoded)
? '+'
: *decoded;
}
*decoded++;
}
*index = '\0';
return encoded;
}
/*******************************************************************************
* URL-Dekodiert Kopie eines Strings.
*
* @param const char* String, der URL-Dekodiert werden soll.
* @return char* URL-Dekodierter String oder NULL
*******************************************************************************/
inline
static
char *
urldecode ( const char *encoded )
{
if (NULL == encoded)
return NULL;
char *decoded = malloc(strlen(encoded));
unsigned index = 0;
if (NULL == decoded)
return NULL;
for (;encoded && *encoded; encoded++, index++)
{
if ('%' == *encoded
&& *(encoded+1)
&& *(encoded+2))
{
decoded[index] = *(encoded+1) >= 65 ? ((*(encoded+1) & 0xDF) - 55) : (*(encoded+1) - 48);
decoded[index] <<= 4;
decoded[index] += *(encoded+2) >= 65 ? ((*(encoded+2) & 0xDF) - 55) : (*(encoded+2) - 48);
encoded += 2;
}
else
decoded[index] = (*encoded == 43 ? 32 : *encoded);
}
decoded[index] = '\0';
return decoded;
}
--
To follow the path: look to the master, follow the master, walk with the master, see through the master, become the master.
Re: hex2ascii
Patrick (Moderator) am 19.11.2010 um 15:35
*decoded++;
in der Funktion urlencode wirft ggf. folgende Warnung:
Zitat:
warning: value computed is not used
Mein Testsystem: gcc version 4.3.2 (Debian 4.3.2-1.1)
Wenn die Warnung beim kompilieren nervt ...
(void)*decoded++;
--
To follow the path: look to the master, follow the master, walk with the master, see through the master, become the master.
[EDIT] - Print Binary Representation
d-503 am 20.09.2011 um 14:43
void printBinRep(char* str){
do{ //Because we like it cryptical (up to some degree)
for(unsigned char i = 128; i || !printf(" "); i >>= 1)
printf("%d", *str & i ? 1 : 0);
}while(*++str || !printf("\n"));
}
--
Über die (angemessen sachliche) Korrektur meiner Fehler freue ich mich stets.
