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

📄 vmhexstringconverter.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   implementation of the functions for hex encoding and decoding
                   of character arrays 

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: 1 $ : $Date: 1/27/99 9:23a $";
/*****************************************************************************/


#include <memory.h>
#include <string.h>
#include "VMHexStringConverter.h"


///////////////////////////////////////////////////////////////////////////////
//
// define the table that defines the mapping between index values and character
// values
//
const char achValues[ 17 ] = "0123456789ABCDEF";


/*****************************************************************************/
/*
     FUNCTION NAME:  VMHexEncoder::BytesToHexString

       DESCRIPTION:  translates an array of bytes to a hex-encoded string. Since
                     each input byte requires two bytes of storage on the output
                     the output buffer must be at least twice as larges as the
                     input buffer

             INPUT:  pchInput   : pointer to the input buffer
                     iInLength  : number of bytes in the input buffer that must
                                  be processed
                     pchOutput  : pointer to the output buffer
                     iOutLength : number of bytes in the output buffer
            OUTPUT:  none

           RETURNS:  number of bytes loaded into the output array
*/
int VMHexEncoder::BytesToHexString( const char* pchInput, int iInLength, char* pchOutput, int iOutLength )
{
  int iNeeded = iInLength * 2 + 2;

  if ( iOutLength < iNeeded )
  {
    return( 0 );
  }
  char chLowNibble;
  char chHighNibble;

  memset( pchOutput, 0, iOutLength );

  int iOffset = 2;

  pchOutput[ 0 ] = '0';
  pchOutput[ 1 ] = 'x';

  for ( int iLoop = 0; 
        ( ( iLoop < iInLength ) && ( iOffset < iOutLength ) ); 
        iLoop++ )
  {
    chLowNibble  = pchInput[ iLoop ] & 0x0F;
    chHighNibble = pchInput[ iLoop ] & 0xF0;

    pchOutput[ iOffset ] = achValues[ chHighNibble ];
    iOffset++;

    pchOutput[ iOffset ] = achValues[ chLowNibble ];
    iOffset++;
  }

  return( iOffset );
}
/* End of function "VMHexEncoder::BytesToHexString"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMHexEncoder::HexStringToBytes

       DESCRIPTION:  translates a hex-encoded string into the original byte 
                     array value represented by the hex string

             INPUT:  pchInput   : pointer to string to translate, must be NULL
                                  terminated
                     pchOuput   : pointer to output buffer.
                     iOutLength : the length of the output buffer
            OUTPUT:  pchOutput : contents are modified

           RETURNS:  number of bytes loaded into the output array
*/
int VMHexEncoder::HexStringToBytes( const char* pchInput, char* pchOutput, int iOutLength )
{
  int iInLength = strlen( pchInput );
  int iNeeded   = iOutLength / 2 - 2;

  // must be an even length
  //
  if ( iInLength % 2 )
  {
    return( 0 );
  }

  // verify output buffer is big enough
  //
  if ( iOutLength < iNeeded )
  {
    return( 0 );
  }

  // check for 'hex marker' on input string
  //
  if ( pchInput[ 0 ] != '0' || pchInput[ 1 ] != 'x' )
  {
    return( 0 );
  }
  
  memset( pchOutput, 0, iOutLength );

  int iOffset = 0;

  for ( int iLoop = 2;
        ( ( iLoop < iInLength ) && ( iOffset <= iOutLength ) );
        iLoop += 2 )
  {
    char chHighNibble = pchInput[ iLoop ];
    char chLowNibble  = pchInput[ iLoop + 1 ];

    int iHighValue = strchr( achValues, chHighNibble ) - achValues;
    int iLowValue  = strchr( achValues, chLowNibble )  - achValues;
    if ( ( iHighValue < 0 ) || ( iLowValue < 0 ) )
    {
      break;
    }
    pchOutput[ iOffset ] = ( ( iHighValue & 0x0F ) << 4 ) | ( iLowValue & 0x0F );
    iOffset++; 
  }
  return( iOffset );
}
/* End of function "VMHexEncoder::HexStringToBytes"
/*****************************************************************************/



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


⌨️ 快捷键说明

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