Blockland Forums > Modification Help

Convert between characters and character indexes (Small Coding Resource)

Pages: (1/2) > >>

Port:


--- Code: ---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 ““;
}

--- End code ---

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.

Ipquarx:

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

Kalphiter:

And?

What the hell is your question?

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

Brian Smithers:

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

Kalphiter:

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.

Pages: (1/2) > >>

Go to full version