Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby and Null-Terminated Strings
    text
    copied!<p>I'm working with the Source Engine protocol currently, writing/porting a library over to use with a future Rails project.</p> <p>My question is...in this reference material, it talks about strings being null-terminated (which, I always thought was '\0').</p> <p>When I go to test this library I wrote (relevant segment attached), it's not working, because in IRB, it's converting my null termination to \u0000 (unicode). How do I fix this to work with the data that's going to be coming in UDP packets? I assume the problem is encoding-related.</p> <pre><code># Get a null ('/0') terminated string from the buffer. def get_string zero_byte = @buffer.index('\0',@position) if zero_byte === nil string = '' else string = get(zero_byte-@position) @position+=1 end string end </code></pre> <p>Basically, it's supposed to find a "null" character, and then take that chunk of data and return it as a string, then move the 'position' in the buffer to the null character so it can be read further.</p> <p>When I run this in ERB, the following happens:</p> <pre><code>2.0.0-p247 :001 &gt; load 'source_buffer.rb' =&gt; true 2.0.0-p247 :002 &gt; b = SourceBuffer.new =&gt; #&lt;SourceBuffer:0x000000023d56d8&gt; 2.0.0-p247 :003 &gt; b.set("Source Engine Query\0") =&gt; 0 2.0.0-p247 :004 &gt; b.get_string =&gt; "" 2.0.0-p247 :005 &gt; b.buffer =&gt; "Source Engine Query\u0000" </code></pre> <p>As you can see, the '\0' I've put in the buffer is being converted to Unicode, and that's breaking my IRB testing.</p> <p>This is going to be used with the following protocol when all is said and done, not sure if that's immediately relevant at this time: </p> <p><a href="https://developer.valvesoftware.com/wiki/Server_queries" rel="nofollow">https://developer.valvesoftware.com/wiki/Server_queries</a></p> <p>Anyway, any help would be greatly appreciated. Thanks a lot, to all who read this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload