⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmbytearray.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            OUTPUT:  

           RETURNS:  void
*/
void VMByteArray::RemoveAt( long lIndex, long lCount )
{
  memmove( m_pData + lIndex, m_pData + lIndex + 1, lCount );
  memset( m_pData + lIndex + 1 + lCount, 0, m_lAllocated - lCount );
  m_lAllocated -= lCount;
}
/* End of function "VMByteArray::RemoveAt"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMByteArray::GetData

       DESCRIPTION:  returns a const pointer to the internal data

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  pointer to the internal array
*/
const BYTE* VMByteArray::GetData( void ) const
{
  return( m_pData );
}
/* End of function "VMByteArray::GetData"
/*****************************************************************************/


///////////////////////////////////////////////////////////////////////////////
//
//                  D E R I V E D   C L A S S   F O L L O W S 
//
///////////////////////////////////////////////////////////////////////////////


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCharByteArray::operator char*

       DESCRIPTION:  returns the internal data as a char*

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  pointer to the internal data as a char *
*/
const char* VMCharByteArray::AsString( void )
{
  return( reinterpret_cast< const char* >( GetData() ) );
}
/* End of function "char*"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCharByteArray::GetCharValue

       DESCRIPTION:  Converts a nibble to a char equivalent

             INPUT:  iByte - the byte to convert
            OUTPUT:  none

           RETURNS:  the equivalent hex char for the given nibble
*/
char VMCharByteArray::GetCharValue( BYTE iByte )
{
  char chReturn = 0;

  BYTE iNibble = iByte & 0x0F;

  switch ( iNibble )
  {
    case 0x00:
      chReturn = '0';
    break;
    case 0x01:
      chReturn = '1';
    break;
    case 0x02:
      chReturn = '2';
    break;
    case 0x03:
      chReturn = '3';
    break;
    case 0x04:
      chReturn = '4';
    break;
    case 0x05:
      chReturn = '5';
    break;
    case 0x06:
      chReturn = '6';
    break;
    case 0x07:
      chReturn = '7';
    break;
    case 0x08:
      chReturn = '8';
    break;
    case 0x09:
      chReturn = '9';
    break;
    case 0x0A:
      chReturn = 'A';
    break;
    case 0x0B:
      chReturn = 'B';
    break;
    case 0x0C:
      chReturn = 'C';
    break;
    case 0x0D:
      chReturn = 'D';
    break;
    case 0x0E:
      chReturn = 'E';
    break;
    case 0x0F:
      chReturn = 'F';
    break;
  }
  return( chReturn );
}
/* End of function "VMCharByteArray::GetCharValue"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCharByteArray::GetNibbleValue

       DESCRIPTION:  converts a hex encoded character as a nibble value

             INPUT:  chChar - the char to convert
            OUTPUT:  none

           RETURNS:  the lower nibble of the given byte will hold the 
                     converted value
*/                   
BYTE VMCharByteArray::GetNibbleValue( char chChar )
{
  BYTE bReturn = (BYTE)0;
  assert( ( ( '0' <= chChar ) && ( '9' >= chChar ) ) 
       || ( ( 'A' <= chChar ) && ( 'F' >= chChar ) ) 
       || ( ( 'a' <= chChar ) && ( 'f' >= chChar ) ) ); 

  switch ( chChar )
  {
    case '0':
      bReturn = 0x00;
    break;
    case '1':
      bReturn = 0x01;
    break;
    case '2':
      bReturn = 0x02;
    break;
    case '3':
      bReturn = 0x03;
    break;
    case '4':
      bReturn = 0x04;
    break;
    case '5':
      bReturn = 0x05;
    break;
    case '6':
      bReturn = 0x06;
    break;
    case '7':
      bReturn = 0x07;
    break;
    case '8':
      bReturn = 0x08;
    break;
    case '9':
      bReturn = 0x09;
    break;
    case 'a':
    case 'A':
      bReturn = 0x0A;
    break;
    case 'b':
    case 'B':
      bReturn = 0x0B;
    break;
    case 'c':
    case 'C':
      bReturn = 0x0C;
    break;
    case 'd':
    case 'D':
      bReturn = 0x0D;
    break;
    case 'e':
    case 'E':
      bReturn = 0x0E;
    break;
    case 'f':
    case 'F':
      bReturn = 0x0F;
    break;
  }
  return( bReturn );
}
/* End of function "VMCharByteArray::GetNibbleValue"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCharByteArray::FromString

       DESCRIPTION:  converts a string to a byte array

             INPUT:  pchString - the string to encode
            OUTPUT:  none

           RETURNS:  contents of this
*/
VMByteArray& VMCharByteArray::FromString( const char* pchString, unsigned int uiTop, bool bCheckForOx )
{
  // first check out the format of the string
  //
  // the length of the string MUST be an even multiple of 2
  //
  if ( ( strlen( pchString ) % 2 ) == 0 )
  { 
    bool bContinue = false;

    // now make sure that the hex marker preceeds the actual data
    //
    if ( true == bCheckForOx )
    {
      if ( ( '0' == *(pchString + 0 ) )
        && ( 'x' == *(pchString + 1 ) ) )
      {
        bContinue = true;
      }
    }
    else
    {
      bContinue = true;
    }

    if ( true == bContinue )
    {
      // since two characters make up a byte (each byte is a nibble's 
      // worth of data), the return set only has to be half as long as
      // the string
      //
      FreeAll();

      if ( true == bCheckForOx )
      {
        SetSize( ( uiTop - 2 ) / 2 );
      }
      else
      {
        SetSize( uiTop / 2 );
      }

      // now loop over the string in 'byte-size increments' (remember each
      // char represents a nibble) and convert character pairs into a single 
      // byte in the output array
      //
      unsigned int iLoop;
      if ( true == bCheckForOx )
      {
        iLoop = 2;
      }
      else
      {
        iLoop = 0;
      }

      while ( iLoop + 1 < uiTop )
      {
        BYTE bTwoChars;

        // get the upper half first, then rotate it and mix in the lower
        // half
        //
        bTwoChars = GetNibbleValue( *( pchString + iLoop ) );

        bTwoChars  = bTwoChars << 4;
        bTwoChars  = bTwoChars & 0xF0;

        bTwoChars |= GetNibbleValue( *( pchString + iLoop + 1 ) );

        Append( bTwoChars );
        iLoop += 2;
      };
    }
  }
  return( *( dynamic_cast< VMByteArray* >( this ) ) );
}
/* End of function "VMCharByteArray::FromString"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCharByteArray::ToString

       DESCRIPTION:  converts this to a string

             INPUT:  pchOutput - pointer to a pre-allocated output buffer
                     lOutBufferSize - the size of the output buffer
                     bStampDataWithOx - if true then "0x" will preceed the data
            OUTPUT:  pchOutput : contents are modified

           RETURNS:  void
*/
void VMCharByteArray::ToString( char* pchOutput, long lOutBufferSize, bool bStampDataWithOx )
{
  // make the output buffer large enough to hold all the returned
  // nibbles of characters
  //
  int iNumBytes;
  if ( bStampDataWithOx )
  {
    iNumBytes = ( GetSize() * 2 ) + 3;
  }
  else
  {
    iNumBytes = ( GetSize() * 2 ) + 1;
  }

  assert( lOutBufferSize >= iNumBytes );

  memset( pchOutput, 0, iNumBytes );

  if ( bStampDataWithOx )
  {
    *(pchOutput + 0 ) = '0';
    *(pchOutput + 1 ) = 'x';
  }
      
  // now loop over the byte array, for each byte, divide it into
  // upper and lower nibbles, then save each nibble as a hex encoded
  // character.
  //
  for( int iLoop = 0; iLoop < GetSize(); iLoop++ )
  {
    const BYTE bElement     = ElementAt( iLoop );
    BYTE bUpperNibble = ( ( ( bElement & 0xF0 ) >> 4 ) & 0x0F );
    BYTE bLowerNibble = bElement & 0x0F;

    int iInsertPosition = strlen( pchOutput );
    *(pchOutput + iInsertPosition )    = GetCharValue( bUpperNibble );
    *(pchOutput + iInsertPosition + 1) = GetCharValue( bLowerNibble );
  }
}
/* End of function "VMCharByteArray::ToString"
/*****************************************************************************/


/*****************************************************************************/
/* Check-in history */
/*
 *$Log:   $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -