Raised This Month: $ Target: $400
 0% 

Code needed for String and Type conversions


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 04-04-2014 , 16:03   Re: Code needed for String and Type conversions
Reply With Quote #3

That ReadString() seems broken or I'm misunderstanding it.

EDIT: This is from nvault_util.inc:
Code:
stock ReadString( szDestString[] , iMaxLen , const SourceData[] , iSourceSize ) {     /*    Memory layout     szDest[ 0 ] = Source{ 3 }     szDest[ 1 ] = Source{ 2 }     szDest[ 2 ] = Source{ 1 }     szDest[ 3 ] = Source{ 0 }     szDest[ 4 ] = Source{ 7 }     szDest[ 5 ] = Source{ 6 }     szDest[ 6 ] = Source{ 5 }     szDest[ 7 ] = Source{ 4 }     szDest[ 8 ] = Source{ 11 }     */             new iDestPos = -1;     new iBytePos = 4;     new iOffset = 1;     new iSourceMax = ( iSourceSize * 4 );         while ( ( ++iDestPos < iMaxLen ) && ( iBytePos < iSourceMax ) )     {         szDestString[ iDestPos ] = SourceData{ iBytePos - iOffset++ };         if ( iDestPos && ( ( iDestPos % 4 ) == 3 ) )         {             iBytePos += 4;             iOffset = 1;         }     }         szDestString[ iDestPos ] = EOS; }

EDIT2: I made these now:
Never seen {} being used to write bytes before, only to define arrays. I had no idea you could do that.
Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 4", "", "[ --{-@ ]");         new Original[] = "Hello World";         server_print("^nOriginal:");     server_print("%s", Original);         new String1[4];     WriteString(String1, charsmax(String1), Original);         server_print("^nWriteString():");     for ( new i ; i < charsmax(String1) && String1[i] ; i++ )         server_print("%d: 0x%08x (%c%c%c%c)", i, String1[i], String1{i * 4} == 0 ? ' ' : String1{i * 4}, String1{i * 4 + 1} == 0 ? ' ' : String1{i * 4 + 1}, String1{i * 4 + 2} == 0 ? ' ' : String1{i * 4 + 2}, String1{i * 4 + 3});         new String2[12];     ReadString(String2, charsmax(String2), String1);         server_print("^nReadString():");     server_print("%s", String2); } stock ReadString(Output[], maxlen, const Input[]) {         new i, len = strlen(Input) * 4;         for ( ; i < len && i < maxlen ; i++ )         Output[i] = Input{i - i % 4 + ( 3 - i % 4 )};         Output[i] = 0; } stock WriteString(Output[], maxlen, const Input[]) {         new len, len2, i;     len = len2 = strlen(Input);     if ( len % 4 )         len2 += 4 - len % 4         for ( ; i < maxlen * 4 && i < len2 ; i++ )             Output{i - i % 4 + ( 3 - i % 4 )} = i < len ? Input[i] : 0;         Output[i / 4] = 0; }

Code:
Original:
Hello World

WriteString():
0: 0x6c6c6548 (lleH)
1: 0x6f57206f (oW o)
2: 0x00646c72 ( dlr)

ReadString():
Hello World
__________________

Last edited by Black Rose; 04-05-2014 at 08:29.
Black Rose is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode