| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| blackmorpheus Expert Cheater
 
 ![]() Reputation: 0 
 Joined: 05 Apr 2008
 Posts: 159
 
 
 | 
			
				|  Posted: Tue Mar 24, 2009 8:09 am    Post subject: ascii to byte |   |  
				| 
 |  
				| how to go from ASCII to Byte? I've got the following code:
 
 
  	  | Code: |  	  | 
 char buf[256];
 
 
 for(;;)
 {
 rc = recv(DestSocket,buf,sizeof(buf), 0);
 printf("Received from Client: %s\n",buf);
 }
 return 0;
 | 
 
 Now i get the input like on the picture.
 
 but I wanna convert it to bytes.
 Like this
 
 Received from Client  hi    => buf[] = {0x48, 0x61};
 So it's easier for me to see what packets have been sent.
 
 
 
 
	
		
	 
		| Description: |  |  
		| Filesize: | 29.25 KB |  
		| Viewed: | 8773 Time(s) |  
		| 
  
 
 |  
 
 Last edited by blackmorpheus on Tue Mar 24, 2009 8:48 am; edited 1 time in total
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Slugsnack Grandmaster Cheater Supreme
 
 ![]() Reputation: 71 
 Joined: 24 Jan 2007
 Posts: 1857
 
 
 | 
			
				|  Posted: Tue Mar 24, 2009 9:03 am    Post subject: |   |  
				| 
 |  
				| not sure which way you wanted to go but it is equally easy to typecast the other way 
 
  	  | Code: |  	  | #include "stdafx.h" #include "windows.h"
 #include <conio.h>
 #include <iostream>
 using namespace std;
 
 int _tmain(int argc, _TCHAR* argv[])
 {
 int buf[] = {0x48, 0x61};
 
 for ( int i = 0; i < (sizeof (buf)/sizeof (int)); i++ )
 cout << (char)buf[i];
 
 while(!_kbhit())
 Sleep(100);
 
 return 0;
 }
 | 
 
 
 
 
	
		
	 
		| Description: |  |  
		| Filesize: | 180.47 KB |  
		| Viewed: | 8754 Time(s) |  
		| 
  
 
 |  
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| blackmorpheus Expert Cheater
 
 ![]() Reputation: 0 
 Joined: 05 Apr 2008
 Posts: 159
 
 
 | 
			
				|  Posted: Tue Mar 24, 2009 9:08 am    Post subject: |   |  
				| 
 |  
				| the other way around is a bit trickier i think.. can u show how?
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Slugsnack Grandmaster Cheater Supreme
 
 ![]() Reputation: 71 
 Joined: 24 Jan 2007
 Posts: 1857
 
 
 | 
			
				|  Posted: Tue Mar 24, 2009 9:18 am    Post subject: |   |  
				| 
 |  
				| how about.. 
 
  	  | Code: |  	  | #include "stdafx.h" #include "windows.h"
 #include <conio.h>
 #include <iostream>
 using namespace std;
 
 int _tmain(int argc, _TCHAR* argv[])
 {
 int buf[] = {0x48, 0x61};
 char charbuf[] = { 'H', 'a' };
 
 for ( int i = 0; i < (sizeof (buf)/sizeof (int)); i++ )
 cout << (char)buf[i];
 
 for ( int i = 0; i < sizeof charbuf; i++ )
 cout << "\n0x" << hex << (int)charbuf[i];
 
 while(!_kbhit())
 Sleep(100);
 
 return 0;
 }
 | 
 
 is that what he wants to do ?
 
 
 
 
	
		
	 
		| Description: |  |  
		| Filesize: | 183.7 KB |  
		| Viewed: | 8742 Time(s) |  
		| 
  
 
 |  
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| blackmorpheus Expert Cheater
 
 ![]() Reputation: 0 
 Joined: 05 Apr 2008
 Posts: 159
 
 
 | 
			
				|  Posted: Tue Mar 24, 2009 10:17 am    Post subject: |   |  
				| 
 |  
				| Thanks all, fixed it. I didn't need the iostream so just used stdio.h.
 
 
 
 
	
		
	 
		| Description: |  |  
		| Filesize: | 68.2 KB |  
		| Viewed: | 8726 Time(s) |  
		| 
  
 
 |  
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		|  |