Conversion between C# string and packed string representation. : Convert « Data Types « C# / C Sharp






Conversion between C# string and packed string representation.

    


using System;
using System.IO;

public class Utils
{
    public static byte[] PackString(String Source)
    {
        byte[] Value = StringToByteArray(Source);
        Int32 ILength = Value.Length;
        byte[] Result = new byte[4 + ILength];
        Result[0] = (byte)((ILength >> 0) & 0xFF);
        Result[1] = (byte)((ILength >> 8) & 0xFF);
        Result[2] = (byte)((ILength >> 16) & 0xFF);
        Result[3] = (byte)((ILength >> 24) & 0xFF);

        for (Int32 i = 0; i < ILength; i++)
            Result[4 + i] = Value[i];
        return Result;

    }

    public static UInt32 UnpackUInt32(byte[] Value)
    {
        return
            (UInt32)(Value[0] << 0) +
            (UInt32)(Value[1] << 8) +
            (UInt32)(Value[2] << 16) +
            (UInt32)(Value[3] << 24);
    }
    public static byte[] StringToByteArray(String Source)
    {

        char[] CSource = Source.ToCharArray();
        byte[] Result = new byte[CSource.Length];

        for (Int32 i = 0; i < CSource.Length; i++)
            Result[i] = Convert.ToByte(CSource[i]);

        return Result;
    }
}

   
    
    
    
  








Related examples in the same category

1.Convert string to int
2.Hidden among the To(integral-type) methods are overloads that parse numbers in another base:
3.The source and target types must be one of the "base" types. ChangeType also accepts an optional IFormatProvider argument. Here's an example:
4.Convert string to byte array with for loop
5.Convert byte array to string
6.Convert a decimal to the binary format
7.Convert.ToString Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base.
8.Convert double to bool
9.Convert double to string
10.Convert console input to double
11.Converts int to string in a specified base.
12.Convert double to int
13.Convert double to bool and string
14.Convert string to char
15.Convert what you read from console into int
16.Convert various data types to integer 64
17.Returns an object of the specified type and whose value is equivalent to the specified object.
18.Convert.ChangeType Method (Object, TypeCode, IFormatProvider)
19.Converts a Unicode character array to an 8-bit unsigned integer array.
20.Convert.ToBase64CharArray() and Convert.FromBase64CharArray
21.Converts byte to Boolean
22.Converts decimal to Boolean value.
23.Converts double to Boolean value.
24.Converts short to Boolean value.
25.Converts int to Boolean value.
26.Converts long to Boolean value.
27.Converts string to Boolean
28.Converts Boolean value to byte.
29.Converts Unicode character to byte
30.Converts decimal number to byte
31.Converts double-precision floating-point number to byte
32.Convert string to bool with exception catch
33.Converts a string of length 4 to a 32-bit unsigned integer.
34.Converts a 32-bit unsigned integer to a string of length 4.
35.Convert object value to double or int