VG008
Senior Member
Hi Guys
I have fixed width string which must be broken down into predefined parts. Using substring is one method of achieving this but IMO, using substring on a 200 character string can become untidy and increase the potential of bugs.
Since I come from a c++ background, I though of using a struct. The code works, but I'm unsure if this is the correct method or what the performance impact will be. Could someone please look at it?
Thank you
Full .cs file can be found @: https://dl.dropboxusercontent.com/u/65420529/MainWindow.xaml.cs
I have fixed width string which must be broken down into predefined parts. Using substring is one method of achieving this but IMO, using substring on a 200 character string can become untidy and increase the potential of bugs.
Since I come from a c++ background, I though of using a struct. The code works, but I'm unsure if this is the correct method or what the performance impact will be. Could someone please look at it?
Thank you
Full .cs file can be found @: https://dl.dropboxusercontent.com/u/65420529/MainWindow.xaml.cs
Code:
/*=======================================================
* Structure
=======================================================*/
unsafe public struct Transaction
{
private const int _RecordIDSize = 2;
private const int _DateSize = 8;
private const int _TimeSize = 6;
private const int _AmountSize = 10;
public fixed char _RecordID[_RecordIDSize];
public fixed char _Date[_DateSize];
public fixed char _Time[_TimeSize];
public fixed char _Amount[_AmountSize];
public Transaction(string RawData)
{
fixed (char* ptrRecordID = _RecordID)
{
char[] chars = RawData.ToCharArray();
Marshal.Copy(chars, 0, new IntPtr(ptrRecordID), chars.Length);
}
}
public string RecordID
{
get
{
fixed (char* c = _RecordID)
{
return CStringToString(new IntPtr(c), Encoding.ASCII, _RecordIDSize);
}
}
}
public string Date
{
get
{
fixed (char* c = _Date)
{
return CStringToString(new IntPtr(c), Encoding.ASCII, _DateSize);
}
}
}
public string Time
{
get
{
fixed (char* c = _Time)
{
return CStringToString(new IntPtr(c), Encoding.ASCII, _TimeSize);
}
}
}
public string Amount
{
get
{
fixed (char* c = _Amount)
{
return CStringToString(new IntPtr(c), Encoding.ASCII, _AmountSize);
}
}
}
}
/*========================================================
* Convert from char array to string variable
========================================================*/
private static unsafe string CStringToString(IntPtr ptr, Encoding encoding, int LengthofCString)
{
void* rawPointer = ptr.ToPointer();
if (rawPointer == null)
return "";
char* unsafeCString = (char*)rawPointer;
int LengthInBytes = encoding.GetByteCount(unsafeCString, LengthofCString);
byte[] asByteArray = new byte[LengthInBytes];
fixed (byte* ptrByteArray = asByteArray)
{
encoding.GetBytes(unsafeCString, LengthofCString, ptrByteArray, LengthInBytes);
}
return encoding.GetString(asByteArray);
}
/*=======================================================
* Example of fixed width string: 01201402101103000000010000
*
* Break Down:
*
* Record ID = 01
* Date = 20140210
* Time = 110300
* Amount = 0000010000
=======================================================*/
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string RawData = string.Empty;
Transaction Trans;
RawData = "01201402101103000000010000";
Trans = new Transaction(RawData);
unsafe
{
MessageBox.Show
(
Trans.RecordID + "\n" + Trans.Date + "\n" + Trans.Time + "\n" + Trans.Amount
);
}
}
