Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Byte To Hex String

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 16, 2010 7:28 am    Post subject: Byte To Hex String Reply with quote

Code:
#include <Windows.h>
#include <stdio.h>

void FormatString( char* szInput, char* szOutput, int nSize ) {
  RtlZeroMemory( szOutput, 3 * nSize + 1 );

  for( int i = 0; i < nSize; i++ ) {
    sprintf_s( &szOutput[i*3], 3, "%02X", szInput[i] );
    szOutput[ ( i + 1 )*3 - 1 ] = ' ';
  }
}

int main() {
  byte lala[] = {0xED};
  int nSize = _countof( lala );
  char* szBuffer = ( char* )malloc( 3 * nSize + sizeof( char ) );
  RtlZeroMemory( szBuffer, 3 * nSize * sizeof( char ) );

  FormatString( ( char* )lala, szBuffer, nSize );

  printf_s( "%s", szBuffer);
  return 0;
}


Why does this code work perfectly for some char values then fails for other ones.. ?! One example of a failing case is 0xED. It fails with an assertion that the buffer is too small on the sprintf_s ARGH


Last edited by Slugsnack on Wed Jun 16, 2010 9:28 am; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Wed Jun 16, 2010 9:06 am    Post subject: Reply with quote

As an alternative, if you don't mind using std::string you could do:

Code:
#include <Windows.h>
#include <string>
#include <stdio.h>

int main( int argc, TCHAR* argv[] )
{
   unsigned char btArray[] = { 0xED };
   int nLength = ( sizeof( btArray ) / sizeof( btArray[ 0 ] ) );

   std::string strHexString;
   for( int x = 0; x < nLength; x++ )
   {
      char szBuff[ 4 ] = { 0 };
      sprintf_s( szBuff, 4, "%02X", btArray[ x ] );

      strHexString += szBuff;
      if( ( x + 1 ) < nLength )
         strHexString += " ";
   }

   printf_s( strHexString.c_str() );

   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 16, 2010 9:44 am    Post subject: Reply with quote

Actually I'd prefer not to use that method, I'm trying more to find out why my method isn't working. Even on yours, I don't understand why you're using 4 as the second parameter for sprintf_s. For a given tchar surely we only need 2 * tchar + 1 for a terminator. So that makes 3 ? And in the case of char, a tchar is just a single byte so the second parameter should be just 3 ?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Wed Jun 16, 2010 11:11 am    Post subject: This post has 2 review(s) Reply with quote

It's because of your conversion of your first param in FormatString, you are forcing the unsigned char array to char* which isn't properly converting it. Pass the unsigned array to the function instead.

Using a similar style to your method:
Code:
#include <Windows.h>
#include <stdio.h>

void FormatString( unsigned char* szInput, LPSTR lpOutput, int nSize )
{
   for( int x = 0; x < nSize; x++ )
   {
      sprintf_s( &lpOutput[ x * 3 ], 3, "%02X", szInput[ x ] );
      lpOutput[ ( x * 3 ) + 2 ] = (char)' ';
   }
}

int main( int argc, TCHAR* argv[] )
{
   unsigned char btArray[] = { 0x8F, 0x99, 0xFF, 0xDC, 0x3F, 0xAA, 0xC9, 0x90, 0x90 };
   int nSize = _countof( btArray );

   char* szBuffer = new char[ nSize * ( 3 + sizeof( char ) ) ];
   memset( szBuffer, 0x00, ( nSize * ( 3 + sizeof( char ) ) ) );

   FormatString( btArray, szBuffer, nSize );
   printf_s( szBuffer );

   delete[] szBuffer;

   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Wed Jun 16, 2010 11:30 am    Post subject: Reply with quote

Works fine here. Is there any special reason why you appear to be stuck in the twilight zone between C and C++?

Code:
#include <Windows.h>
#include <stdio.h>

bool FormatString(unsigned char const * szInput, char * szOutput, unsigned int nSize) {
   if(szInput == 0 || szOutput == 0 || nSize == 0) {
      return false;
   }
   for(unsigned int i = 0; i < nSize; ++i) {
      if(sprintf_s(&szOutput[i*3], 3, "%02X", szInput[i]) == -1) {
         return false;
      }
      szOutput[(i + 1)*3 - 1] = ' ';
   }
   szOutput[nSize*3] = '\0';
   return true;
}

int main() {
   byte lala[] = { 0xED, 0xE3 };
   int nSize = _countof(lala);
   char* szBuffer = (char*)malloc(3 * nSize + sizeof(char));
   if(FormatString(lala, szBuffer, nSize)) {
      printf_s("%s\n", szBuffer);
   } else {
      printf_s("Something fucked up.\n");
   }
   return 0;
}
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jun 16, 2010 2:25 pm    Post subject: Reply with quote

Thanks you 2.. I didn't even know a char can be signed/unsigned. Like what.. that is completely counter-intuitive lol.

Flyte : nah not stuck, it's just this weird preference I have. I learnt programming like this..
x86 -> some C -> Haskell -> Java -> Prolog -> C -> C++ -> MIPS -> Q

Then there's some other crap and a bunch of random languages along the way. Idk why I dislike coding OOP in C++ so much. Whenever I feel like coding OOP I just code Java instead. I've worked on several projects recently that would've been so much easier in C++ and suited to OOP but I deliberately used C as opposed to C++. For example.. writing an implementation of linked lists instead of using vector. C++ 'looks' messy to me in terms of looking at the source and I have a big obsession with code elegance.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jun 17, 2010 2:21 am    Post subject: Reply with quote

I don't disagree that C++ has some curious syntax sometimes but it's nice when your clusterfuck starts coming together. Some objects here, some magic there, all of a sudden there's a framework and everything works.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites