Author Topic: Convert between characters and character indexes (Small Coding Resource)  (Read 1818 times)

Code: [Select]
function charToIdx( %char )
{
          if ( strLen( %char ) != 1 )
          {
                    return -1;
          }
         
          %count = 16;
          %hex = “0123456789ABCDEF“;
         
          for ( %x = 0 ; %x < %count ; %x++ )
          {
                    for ( %y = 0 ; %y < %count ; %y++ )
                    {
                              if ( strStr( %char, collapseEscape( “\\x“ @ getSubStr( %hex, %x, 1 ) @ getSubStr( %hex, %y, 1 ) ) ) == 0 )
                              {
                                        return %x + %y;
                              }
                    }
          }
         
          return -1;
}

function idxToChar( %idx )
{
          if ( !strLen( %idx ) || %idx < 0 || %idx > 254 )
          {
                    return ““;
          }
         
          %count = 16;
          %hex = “0123456789ABCDEF“;
         
          for ( %x = 0 ; %x < %count ; %x++ )
          {
                    for ( %y = 0 ; %y < %count ; %y++ )
                    {
                              if ( %x + %y == %idx )
                              {
                                        return collapseEscape( “\\x“ @ getSubStr( %hex, %x, 1 ) @ getSubStr( %hex, %y, 1 ) );
                              }
                    }
          }
         
          return ““;
}

Sorry for the lack of proper identation, I coded this on a tiny phone keyboard.

charToIdx( char ) => ASCII index of char (0-254).
idxToChar( idx ) => Character representation of the ASCII index.
« Last Edit: January 14, 2012, 01:28:58 AM by Port »

so if you type in 255 it should return the display value of \xFF?

And?

What the hell is your question?

If it's a syntax error, it's most likely those quotes that are “ and not "

Your a dolt kalphiter. It says in the title it's a Coding Resource.

Either way, all the “ in there will be a problem.

It's in the wrong board, so I didn't think it would've been a resource.

Why is the loop necessary in the second one? I had something like these which I haven't used much yet:

Code: [Select]
function base10toHex(%num)
{
  %s = "0123456789ABCDEF";
  %num = mFloor(%num);
  return getSubStr(%s,(%num - (%num % 16)) / 16,1) @ getSubStr(%s,(%num % 16),1);
}

function numToChar(%x)
{
  %num = base10toHex(%x);
  eval("%char = \"\\x" @ %num @ "\";");
  return %char;
}

I didn't know that collapseEscape existed for this purpose, so this looks like it'd be shorter:
Code: [Select]
function numToChar(%x)
{
  %num = base10toHex(%x);
  %char = collapseEscape("\\x" @ %num);
  return %char;
}

My solution for the first one was just a few large switch statements (case "\x51": return 81; works with input charToNum("Q")) so whether that's faster than the loop or not I don't know. Incidentally a switch statement errors after about 83 cases and doesn't work.

They should both test for 16*%x + %y otherwise it's getting most of the cases wrong.
« Last Edit: January 14, 2012, 11:52:40 AM by Space Guy »

I had this from 2 years ago, and I chose the name "chr" because that's the equivalent in PHP.

Code: [Select]
function dec2hex(%val) 
{
    %digits ="0123456789ABCDEF";
    %firstDigit = getSubStr(%digits, mFloor(%val/16), 1);
    %secDigit = getSubStr(%digits, %val % 16, 1);   
    return %firstDigit @ %secDigit; 
}

function chr(%num)
{
    %rand = getRandom();
    eval("$chrString["@ %rand @"] = \"\\x"@ dec2hex(%num) @"\";");
    %return = $chrString[%rand];
    $chrString[%rand] = "";
    return %return;
}
The %rand is just for a random string to retrieve later, it could be made more efficient.
For example, when you convert a number to a character, you can store the number in an array with character as the index, so that you don't have to loop through everything again.
« Last Edit: January 14, 2012, 12:14:47 PM by Kalphiter »