Chapter Contents

Previous

Next
memxlt

memxlt



Translate a Block of Memory

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lcstring.h>

void *memxlt(void *blk, const char *table, size_t n);


DESCRIPTION

memxlt translates a block of memory from one character set to another. The first argument ( blk ) is the address of the area of memory to be translated, and the third argument ( n ) is the number of characters to be translated. The second argument ( table ) is a pointer to a 256-byte translate table, which should be defined so that table[c] for any character c is the value to which c should be translated. (The function xltable is frequently used to build such a table.)

Note:    The argument string is translated in place; that is, each character in the string is replaced by a translated character.  [cautionend]


RETURN VALUE

memxlt returns a pointer to the translated string.


CAUTION

The third argument to memxlt is size_t . If a negative number is passed, massive overlaying of memory occurs.

The effect of memxlt is not defined if the source string and the translate table overlap.


IMPLEMENTATION

If the number of bytes to be translated is a small integer constant (less than or equal to 256), the compiler generates inline code for memxlt unless the function is undefined (by an #undef statement) to prevent this.


EXAMPLE

This example produces a secret word using memxlt . The argument word specified on the command line is translated using a randomly arranged alphabet. The translated message is then printed to stdout :

#include <lcstring.h>
#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[])
{
   int len, i, j;
   char a;
   char alphabet[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   char table[256];
   if (argc < 2) {
      puts("Specify the secret word on the command line.");
      exit(4);
   }

   len = strlen(argv[1] );
   memupr(argv[1], len);     /* Uppercase input message. */

      /* Randomize the alphabet.                         */
   for (i = 0; i < 26; i++)
      for (j = i + 1; j < 26; j++)
         if (rand() % 2) {
            a = alphabet[i];
            alphabet[i]  = alphabet[j] ;
            alphabet[j]  = a;
         }

      /* Build a translate table.                        */
   xltable(table,"ABCDEFGHIJKLMNOPQRSTUVWXYZ",alphabet);

      /* Translate message.                              */
   memxlt(argv[1],table,len);

      /* Print message.                                  */
   printf("Today's secret word is: \n%s\n",argv[1]);
   return;
}


RELATED FUNCTIONS

memlwr , memupr , xltable


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.