Re: Convert native character string to ASCII array of integers
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 28 Mar 2008 11:46:25 +0000
Tomás Ó hÉilidhe said:
<snip>
Please my MakeASCII function! Rip it apart!
Well, I won't rip it apart, but I think I can let a little air out of it.
#include <string.h>
void MakeASCII(unsigned char *pos,char const *pc)
{
const char *bcs =
" !\"#$%&'()*+,-./0123456789:;<=>?@"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"[\\]^_`"
"abcdefghijklmnopqrstuvwxyz"
"{|}~";
const char *cur = NULL;
while(*pc != '\0')
{
cur = strchr(bcs, *pc);
if(cur != NULL)
{
*pos++ = (cur - bcs) + 32;
}
else
{
*pos++ = *pc;
}
++pc;
}
*pos = '\0';
}
If you hit performance issues with that one, consider this alternative:
#include <string.h>
#include <limits.h>
void MakeASCII(unsigned char *pos,char const *pc)
{
const char *bcs =
" !\"#$%&'()*+,-./0123456789:;<=>?@"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"[\\]^_`"
"abcdefghijklmnopqrstuvwxyz"
"{|}~";
static char att[UCHAR_MAX + 1] = {0};
const char *cur = bcs;
int i = 0;
if(att[' '] != 32) /* do we need to set up the array? */
{
/* defaults */
for(i = 0; i < UCHAR_MAX + 1; i++)
{
att[i] = (char)i;
}
/* known ASCII characters */
i = 32;
while(*cur != '\0')
{
att[*cur++] = i++;
}
}
while(*pos++ = att[*pc++])
{
continue;
}
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: Convert native character string to ASCII array of integers
- From: Tomás Ó hÉilidhe
- Re: Convert native character string to ASCII array of integers
- References:
- Convert native character string to ASCII array of integers
- From: Tomás Ó hÉilidhe
- Convert native character string to ASCII array of integers
- Prev by Date: Re: bb king, lolita nude, nude children, child super models, loli bbs
- Next by Date: Re: little lolitas, bbs, pthc, lolita art, lolita pics
- Previous by thread: Convert native character string to ASCII array of integers
- Next by thread: Re: Convert native character string to ASCII array of integers
- Index(es):
Relevant Pages
|