In my case it is quite possible for 0xFF to occur, so I ended up doing, as I 
mentioned previously, a sort of base-128 workaround like below.  It works, it's 
just really lame.  Oh well, thanks though.

my_uint16_t my_htons(my_uint16_t i) {
    assert(i < 255*255);

    my_uint16_t encoded_i = 0;
    my_uint16_t rem_i;

    rem_i = i % 255;
    i /= 255;
    assert(rem_i <= 254);
    *((unsigned char*)(&encoded_i) + 0) = (unsigned char)(rem_i + 1);
    assert(*((unsigned char*)(&encoded_i) + 0) != 0);

    rem_i = i % 255;
    assert(i < 255);
    assert(rem_i <= 254);
    *((unsigned char*)(&encoded_i) + 1) = (unsigned char)(rem_i + 1);
    assert(*((unsigned char*)(&encoded_i) + 1) != 0);

    return encoded_i;
}


At 2007/11/11 03:32 PM, Haza wrote:
>Try these two functions. Just wrote them then in like 3min, haven't tested
>them. It allows you to send wchar_t strings.
>
>All it really does is substitutes 0x00 for 0xFF. So you loose the use of
>0x00FF and 0xFFFF characters.
>
>I think I forgot to check for wchar_t string ending in the Encode function,
>but I'm sure you can do it.
>
>~Haza
>
>
>void NetSafe16Encode( wchar_t *in, char *out )
>{
>        char ZeroByte = (char)0xFF;
>
>        int out_counter = 0;
>        for(int i = 0; i < (sizeof(in)/sizeof(wchar_t)); i++)
>        {
>                // Use bit shifting operations to clean up data.
>                char right = (char)((in[i] << 8) >> 8);
>                if( right == (char)0x00 )
>                        right = ZeroByte;
>
>                char left = (char)(in[i] >> 8);
>                if( left == (char)0x00 )
>                        left = ZeroByte;
>
>                out[out_counter] = left;
>                out_counter++;
>                out[out_counter] = right;
>                out_counter++;
>        }
>}
>
>void NetSafe16DeEncode( char *in, wchar_t *out )
>{
>        char ZeroByte = (char)0xFF;
>
>        int out_counter = 0;
>        for(int i = 0; i < sizeof(in); i++)
>        {
>                unsigned short left;
>                if(in[i] == ZeroByte)
>                        left = 0x0000;
>                else
>                        left = ((unsigned short)in[i]) << 8;
>
>                i++;
>
>                unsigned short right;
>                if(in[i] == ZeroByte)
>                        right = 0x0000;
>                else
>                        right = ((unsigned short)in[i]);
>
>                wchar_t final = (wchar_t)(left + right);
>
>                out[out_counter] = final;
>
>                out_counter++;
>        }
>}
>
>
>_______________________________________________
>To unsubscribe, edit your list preferences, or view the list archives, please 
>visit:
>http://list.valvesoftware.com/mailman/listinfo/hlcoders

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to