Commit f2a2a4e6 authored by Brickner_cp's avatar Brickner_cp

DataSegment.ToString() now prints the length and the first 10 bytes of the segment.

Added DataSegment.ToHexadecimalString() for the full hexadecimal string.
Renamed DataSegment.ToString(Encoding) to Decode.
parent 07075fab
......@@ -399,7 +399,7 @@ namespace PcapDotNet.Core.Test
case DnsType.Txt: // 16.
case DnsType.Spf: // 99.
var txtData = (DnsResourceDataText)data;
dataField.AssertShow("Text: " + txtData.Text[_txtIndex++].ToString(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
dataField.AssertShow("Text: " + txtData.Text[_txtIndex++].Decode(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
dataField.AssertNoFields();
break;
......@@ -444,7 +444,7 @@ namespace PcapDotNet.Core.Test
case DnsType.X25:
dataField.AssertName("");
dataField.AssertShow("PSDN-Address: " + ((DnsResourceDataString)data).String.ToString(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
dataField.AssertShow("PSDN-Address: " + ((DnsResourceDataString)data).String.Decode(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
dataField.AssertNoFields();
break;
......@@ -455,12 +455,12 @@ namespace PcapDotNet.Core.Test
{
case "ISDN Address":
dataField.AssertShow(dataFieldShowUntilColon + ": " +
isdnData.IsdnAddress.ToString(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
isdnData.IsdnAddress.Decode(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
break;
case "Subaddress":
dataField.AssertShow(dataFieldShowUntilColon + ": " +
isdnData.Subaddress.ToString(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
isdnData.Subaddress.Decode(EncodingExtensions.Iso88591).ToWiresharkLiteral(false, false));
break;
default:
......
......@@ -132,10 +132,25 @@ namespace PcapDotNet.Packets
}
/// <summary>
/// Converts the segment to a hexadecimal string representing every bytes as two hexadecimal digits.
/// Creates a string starting with the number of bytes the data segment contains
/// and ending with the first 10 bytes of the data segment as hexadecimal digits.
/// </summary>
/// <returns>A hexadecimal string representing every bytes as two hexadecimal digits.</returns>
/// <returns>
/// A string starting with the number of bytes the data segment contains
/// and ending with the first 10 bytes of the data segment as hexadecimal digits.
/// </returns>
public sealed override string ToString()
{
const int MaxNumBytesToUse = 10;
return string.Format("{0} bytes: {1}{2}", Length, Buffer.Range(StartOffset, Math.Min(Length, MaxNumBytesToUse)).BytesSequenceToHexadecimalString(),
(Length > MaxNumBytesToUse ? "..." : ""));
}
/// <summary>
/// Converts the segment to a hexadecimal string representing every byte as two hexadecimal digits.
/// </summary>
/// <returns>A hexadecimal string representing every byte as two hexadecimal digits.</returns>
public string ToHexadecimalString()
{
return Buffer.Range(StartOffset, Length).BytesSequenceToHexadecimalString();
}
......@@ -145,7 +160,7 @@ namespace PcapDotNet.Packets
/// </summary>
/// <param name="encoding">The encoding to use to convert the bytes sequence in the segment to a string.</param>
/// <returns>A string of the bytes in the segment converted using the given encoding.</returns>
public string ToString(Encoding encoding)
public string Decode(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException("encoding");
......
......@@ -74,7 +74,7 @@ namespace PcapDotNet.Packets.Dns
public override string ToString()
{
if (_utf8 == null)
_utf8 = _labels.Select(label => label.ToString(Encoding.UTF8)).SequenceToString('.') + ".";
_utf8 = _labels.Select(label => label.Decode(Encoding.UTF8)).SequenceToString('.') + ".";
return _utf8;
}
......
......@@ -127,19 +127,19 @@ namespace PcapDotNet.Packets.Dns
int longtitudeNumBytes = data[0];
if (data.Length < longtitudeNumBytes + 3)
return null;
string longtitude = data.Subsegment(1, longtitudeNumBytes).ToString(Encoding.ASCII);
string longtitude = data.Subsegment(1, longtitudeNumBytes).Decode(Encoding.ASCII);
data = data.Subsegment(longtitudeNumBytes + 1, data.Length - longtitudeNumBytes - 1);
int latitudeNumBytes = data[0];
if (data.Length < latitudeNumBytes + 2)
return null;
string latitude = data.Subsegment(1, latitudeNumBytes).ToString(Encoding.ASCII);
string latitude = data.Subsegment(1, latitudeNumBytes).Decode(Encoding.ASCII);
data = data.Subsegment(latitudeNumBytes + 1, data.Length - latitudeNumBytes - 1);
int altitudeNumBytes = data[0];
if (data.Length != altitudeNumBytes + 1)
return null;
string altitude = data.Subsegment(1, altitudeNumBytes).ToString(Encoding.ASCII);
string altitude = data.Subsegment(1, altitudeNumBytes).Decode(Encoding.ASCII);
return new DnsResourceDataGeographicalPosition(longtitude, latitude, altitude);
}
......
......@@ -53,7 +53,7 @@ namespace PcapDotNet.Packets.Http
{
Datagram bytesToken;
Token(out bytesToken);
token = Success ? bytesToken.ToString(Encoding.ASCII) : null;
token = Success ? bytesToken.Decode(Encoding.ASCII) : null;
return this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment