It is currently Thu Jun 20, 2013 10:38 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: String/Int To Byte
PostPosted: Tue Jul 03, 2012 11:19 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Apr 20, 2011 4:24 pm
Posts: 156
I been constructing my packets in memory. Now I am lost when it comes to breaking down a string and adding each peice to the buffer byte by byte. What I am trying to accomplish is converting a preference file read string, and changing it into a byte array. So that I can add it to my packet.

Example:

Code:
  IP.s = ReadPreferenceString("IP", "127.0.0.1")

  ;Convert String To Bytes

  PokeB(*Packet + 51, $31)
  PokeB(*Packet + 52, $32)
  PokeB(*Packet + 53, $37)
  PokeB(*Packet + 54, $2E)
  PokeB(*Packet + 55, $30)
  PokeB(*Packet + 56, $2E)
  PokeB(*Packet + 57, $30)
  PokeB(*Packet + 58, $2E)
  PokeB(*Packet + 59, $31)


I have tried adding it to the buffer as a string using PokeS() but the client doesn't seem to respond to the packet if I do it like that. I am trying to do this with an integer too, the client doesn't seem to like packets unless they are made up entirely of bytes.

_________________
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Tue Jul 03, 2012 11:42 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1411
Location: Boston, MA
Pause your program and look at the memory with
Code:
ShowMemoryViewer(*Packet,64)
CallDebugger

Choose the byte table and then choose the string table to confirm your memory is as you like?

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Tue Jul 03, 2012 11:48 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2427
Location: Utah, USA
Warmonger wrote:
I have tried adding it to the buffer as a string using PokeS() but the client doesn't seem to respond to the packet if I do it like that. I am trying to do this with an integer too, the client doesn't seem to like packets unless they are made up entirely of bytes.

The values are always bytes, even floating point is just a collection of bytes.

It is probably the length of the string, usually indicated in PureBasic by the ending Null. If you are forming a packet do you indicate the length of the string, followed by the bytes for the string data or are you placing the bytes of string data followed by a Null?

If you are doing the first one, the client reads the length and then retrieves that number of additional bytes for the string data.
If you are doing the second one, the client reads the string data until a Null is read.

_________________
Image


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Wed Jul 04, 2012 12:23 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Apr 20, 2011 4:24 pm
Posts: 156
Demivec wrote:
Warmonger wrote:
I have tried adding it to the buffer as a string using PokeS() but the client doesn't seem to respond to the packet if I do it like that. I am trying to do this with an integer too, the client doesn't seem to like packets unless they are made up entirely of bytes.

The values are always bytes, even floating point is just a collection of bytes.

It is probably the length of the string, usually indicated in PureBasic by the ending Null. If you are forming a packet do you indicate the length of the string, followed by the bytes for the string data or are you placing the bytes of string data followed by a Null?

If you are doing the first one, the client reads the length and then retrieves that number of additional bytes for the string data.
If you are doing the second one, the client reads the string data until a Null is read.


Yea, I know that much. Every piece of data is represented as a byte. It just must of been how I used it. This seems to work.

Code:
PokeS(*Packet + 51, G_IP, Len(G_IP), #PB_UTF8)


Edit: Now trying to figure out a unicode/character... Trying to write a port into memory.

Code:
G_Port = 4110

PokeU(*Packet + 63, G_Port)


And when I sniff the packet it comes out as 1010 instead of 100E.

_________________
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Wed Jul 04, 2012 1:15 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Sep 20, 2004 7:12 am
Posts: 297
Location: Hell
Warmonger wrote:
Edit: Now trying to figure out a unicode/character... Trying to write a port into memory.

Code:
G_Port = 4110

PokeU(*Packet + 63, G_Port)


And when I sniff the packet it comes out as 1010 instead of 100E.


Not really:
Code:
*Packet = AllocateMemory(1024)
G_Port = 4110

PokeU(*Packet + 63, G_Port)
Debug Hex(PeekU(*Packet + 63), #PB_Unicode)


I guess your problem is somewhere else.

_________________
Please, don't bump bug reports!
(Let them rest in peace...)


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Wed Jul 04, 2012 1:54 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Apr 20, 2011 4:24 pm
Posts: 156
HeX0R wrote:
Warmonger wrote:
Edit: Now trying to figure out a unicode/character... Trying to write a port into memory.

Code:
G_Port = 4110

PokeU(*Packet + 63, G_Port)


And when I sniff the packet it comes out as 1010 instead of 100E.


Not really:
Code:
*Packet = AllocateMemory(1024)
G_Port = 4110

PokeU(*Packet + 63, G_Port)
Debug Hex(PeekU(*Packet + 63), #PB_Unicode)


I guess your problem is somewhere else.


Code:
  G_Port = 4110

  PokeB(*Packet + Pos + 1, $00)
  PokeB(*Packet + Pos + 2, $00)
  PokeB(*Packet + Pos + 3, $00)
 
  ;Port
  PokeU(*Packet + Pos + 4, G_Port)
 
  PokeB(*Packet + Pos + 6, $00)
  PokeB(*Packet + Pos + 7, $00)
  PokeB(*Packet + Pos + 8, $00)
  PokeB(*Packet + Pos + 9, $00)

Ends up:
Code:
00 00 10 10 00 00 00 00

when sent via SendNetworkData(). It should be:
Code:
00 00 10 0E 00 00 00 00

I've tried to figure this out several times before, think I came to the conclusion a few days ago that it was an internal bug with PB.

_________________
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Wed Jul 04, 2012 7:22 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1471
Location: Germany
Hi,
Code:
*Packet = AllocateMemory(100)

G_Port = 4110
Pos = 0

PokeB(*Packet + Pos + 1, $00)
PokeB(*Packet + Pos + 2, $00)
PokeB(*Packet + Pos + 3, $00)

;Port
PokeU(*Packet + Pos + 4, G_Port)

PokeB(*Packet + Pos + 6, $00)
PokeB(*Packet + Pos + 7, $00)
PokeB(*Packet + Pos + 8, $00)
PokeB(*Packet + Pos + 9, $00)

For i = 0 To 9
  Debug RSet(Hex(PeekB(*Packet + i), #PB_Byte), 2, "0")
Next i

FreeMemory(*Packet)
Results in:
Code:
00 00 00 00 0E 10 00 00 00 00
0x100E is correct!
What you need is to convert this stuff in network byte order (Big-Endian).
But I can not see 0x1010

Bernd

P.S.: Maybe the 0x0E is overwritten by something else.


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Wed Jul 04, 2012 8:24 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 2506
Location: New Zealand
Code:
IP.s =  ReadPreferenceString("IP", "127.0.0.1")
*buf =AllocateMemory( StringByteLength(IP) + SizeOf(Character))
PokeS(*buf,IP)
;at other end
Debug PeekS(*buf)


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Thu Jul 05, 2012 5:41 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Apr 20, 2011 4:24 pm
Posts: 156
infratec wrote:
Hi,
Code:
*Packet = AllocateMemory(100)

G_Port = 4110
Pos = 0

PokeB(*Packet + Pos + 1, $00)
PokeB(*Packet + Pos + 2, $00)
PokeB(*Packet + Pos + 3, $00)

;Port
PokeU(*Packet + Pos + 4, G_Port)

PokeB(*Packet + Pos + 6, $00)
PokeB(*Packet + Pos + 7, $00)
PokeB(*Packet + Pos + 8, $00)
PokeB(*Packet + Pos + 9, $00)

For i = 0 To 9
  Debug RSet(Hex(PeekB(*Packet + i), #PB_Byte), 2, "0")
Next i

FreeMemory(*Packet)
Results in:
Code:
00 00 00 00 0E 10 00 00 00 00
0x100E is correct!
What you need is to convert this stuff in network byte order (Big-Endian).
But I can not see 0x1010

Bernd

P.S.: Maybe the 0x0E is overwritten by something else.


Strange it works now. Must be I have setup the Pos wrong carrying the pointer from the beginning of the packet. Here is how I have it now and works (bytes need to be reversed back in order).

Code:
PokeU(*Packet + Pos + 4, G_Port)
PByte1 = PeekB(*Packet + Pos + 4)
PByte2 = PeekB(*Packet + Pos + 5)
PokeB(*Packet + Pos + 4, PByte2)
PokeB(*Packet + Pos + 5, PByte1)


Example:

Code:
*Packet = AllocateMemory(100)

G_Port = 4110
Pos = 0

PokeB(*Packet + Pos + 1, $00)
PokeB(*Packet + Pos + 2, $00)
PokeB(*Packet + Pos + 3, $00)

;Port
PokeU(*Packet + Pos + 4, G_Port)

PokeB(*Packet + Pos + 6, $00)
PokeB(*Packet + Pos + 7, $00)
PokeB(*Packet + Pos + 8, $00)
PokeB(*Packet + Pos + 9, $00)

PokeU(*Packet + Pos + 4, G_Port)
PByte1 = PeekB(*Packet + Pos + 4)
PByte2 = PeekB(*Packet + Pos + 5)
PokeB(*Packet + Pos + 4, PByte2)
PokeB(*Packet + Pos + 5, PByte1)

For i = 0 To 9
  Debug RSet(Hex(PeekB(*Packet + i), #PB_Byte), 2, "0")
Next i

_________________
Its Not A Bug, Its An Undocumented Feature!
Relax Its All Just Ones And Zeros
There Is No Place Like 127.0.0.1 Except ::1
I do things TO my computer, not WITH my computer... I am a nerd.


Top
 Profile  
 
 Post subject: Re: String/Int To Byte
PostPosted: Thu Jul 05, 2012 7:35 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1471
Location: Germany
Hi,

for your BigEndian problem:
Code:
*Packet = AllocateMemory(100)

G_Port = 4110
Pos = 0

PokeB(*Packet + Pos + 1, $00)
PokeB(*Packet + Pos + 2, $00)
PokeB(*Packet + Pos + 3, $00)

;Port
PokeB(*Packet + Pos + 4, G_Port >> 8)   ; High byte first
PokeB(*Packet + Pos + 5, G_Port & $FF)  ; Low byte

PokeB(*Packet + Pos + 6, $00)
PokeB(*Packet + Pos + 7, $00)
PokeB(*Packet + Pos + 8, $00)
PokeB(*Packet + Pos + 9, $00)

For i = 0 To 9
  Debug RSet(Hex(PeekB(*Packet + i), #PB_Byte), 2, "0")
Next i
Bernd


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: epog10 and 5 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye