> From: Tan Kuan Eeik
>
> The reason i pass both the Char* and MemHandle is to enable me to
> resize it in hexdump .... for experimental purpose...
>

So, here is the same code rewritten to pass in an unlocked handle instead of
a pointer.  Use whichever version you want.  (This is the last time I'm
going to write this code for you.)

// My version of hexdump, without hextxt or slen params.
// Creates a hex version of stxt, and returns it in hexHandle.
static void hexdump2(Char* stxt, MemHandle hexHandle)
{
  MemHandle hhnd;
  Char* htxt, *hextxt;
  Int16 i, slen;
  Char hs[] = "0123456789ABCDEF";

  if (!stxt || StrLen(stxt) == 0)
  { // just copy '\0'
    hextxt = (Char *)MemHandleLock(hexHandle);
    StrCopy(hextxt, "");
    MemHandleUnlock(hhnd);
  }
  else
  {
    // allocate enough memory for the string and lock it
    slen = StrLen(stxt);
    hhnd = MemHandleNew(3*(slen+1));
    htxt = (Char *)MemHandleLock(hhnd);

    // build the hex string
    for(i=0;i<slen;++i) {
      htxt[3*i] = hs[((unsigned char)stxt[i])>>4];
      htxt[3*i+1] = hs[((unsigned char)stxt[i])&15];
      htxt[3*i+2] = ' ';
    }
    htxt[3*slen-1] = '\0';

    // resize hexHandle, get a pointer to it, and copy the string
    MemHandleResize(hexHandle, 3*(slen+1));
    hextxt = (Char *)MemHandleLock(hexHandle);
    StrCopy(hextxt, htxt);

    // clean up
    MemHandleUnlock(hhnd);
    MemHandleFree(hhnd);
    MemHandleUnlock(hexHandle);
  }
}

// code to call hexdump2
static void myTest2(void)
{
  Char *phtxt, *ptxt;
  UInt16 plen;
  MemHandle phandle;

  ptxt = "123456789";

  // get space for the hex string
  plen = StrLen(ptxt);
  phandle = MemHandleNew(plen*3+1);

  hexdump2(ptxt, phandle);

  phtxt = MemHandleLock(phandle);
  FrmCustomAlert(MyAlertAlert, "crypto() - 5", ptxt, " ");
  FrmCustomAlert(MyAlertAlert,"crypto() - 10", " ", phtxt);
  MemHandleUnlock(phandle);
  MemHandleFree(phandle);
}


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to