peer code review/advice needed for noob programmer
- From: Stick <Stick@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 22 May 2007 00:49:00 -0700
Hi,
I have the following function which works, but I want to do two things in
updating it.
1) I want to convert from sprintf() to sprintf_s() etc. to end warnings
2) I want to understand how one properly gets the size (vs. what I might
be doing)
and finally:
3) I compile on VS 2005 SP1, on 32 bit Windows XP Home SP2, and want to
know if I can compile with options to target a 64-bit platform so that this
function would run properly on Vista 64. (If possible) Actually, though,
this last is not going to be used as this program is part of a game add-on
that will run in a 32-bit game even when under Vista, so it is really a
non-issue. I just want to educate myself more.
Anyway, here is the function:
RETVALUE CMyClass::Buffer2HexString( const BYTE* pBuffer, const unsigned int
pBufferSize, char** pString )
{
RETVALUE lReturnValue = SUCCESS;
unsigned int uOutStringSize = pBufferSize*2 + 2 + 1;
// Caller must delete[] this pointer
char* sOutString = new char[uOutStringSize];
//sprintf( sOutString, "%s", "" );
sprintf_s( sOutString, uOutStringSize, "%s", "" );
for( unsigned int i = 0; i < pBufferSize / 2 ; i++ )
{
for ( int j = 0; j < 2; j++ )
{
// 4 chars per group
BYTE b = pBuffer[ 2 * i + j ];
// NOTE: 2 chars in out string for each byte in input buffer
byte ch = 0x00;
char hexString[] = "00"; // NULL terminated
char pseudo[] = {"0123456789ABCDEF"};
ch = (byte) (b & 0xF0); // Strip off high nibble
ch = (byte) (ch >> 4); // Shift the bits down
ch = (byte) (ch & 0x0F); // In case high order bit set
hexString[0] = pseudo[ (int)ch ]; // Nibble to hex
ch = (byte) (b & 0x0F); // Strip off low nibble
hexString[1] = pseudo[ (int)ch ]; // Nibble to hex
hexString[2] = NULL;
//strcat( sOutString, hexString );
strcat_s( sOutString, uOutStringSize, hexString );
}
if ( i < ( pBufferSize / 2 - 1 ) )
{
strcat_s( sOutString, uOutStringSize, "-" );
}
}
*pString = sOutString;
return lReturnValue;
}
This function was adapted from one I found online, and I have not
"corrected" the var names, so if you comments on better more "Code Complete"
names please let me know what you think too. I shall be thick skinned. =)
All critisim appreciated.
Thanks, Patrick
.
- Follow-Ups:
- Re: peer code review/advice needed for noob programmer
- From: Joseph M . Newcomer
- Re: peer code review/advice needed for noob programmer
- Prev by Date: Re: Compilator C++ freeware for commercial use
- Next by Date: Re: Send a string between 2 applications
- Previous by thread: Compilator C++ freeware for commercial use
- Next by thread: Re: peer code review/advice needed for noob programmer
- Index(es):
Relevant Pages
|
Loading