Fixed Width Byte[] C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to create a formatted byte array for SMPP, e.g.:

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

[00 00 00 21] is the length of the entire message in octets, however the
value is only in the 4 octet, is there someway I can format this into fixed
width, a comparison would be

string s = "21";
Console.WriteLine(s.PadLeft(4,'0'));

Result: 0021

Currently my result is as follows(the value of 15 changes because it is the
octet length

15 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I need it to look like

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

Any assistance would be greatly appreciated.
 
Michael said:
I need to create a formatted byte array for SMPP, e.g.:

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

[00 00 00 21] is the length of the entire message in octets, however the
value is only in the 4 octet, is there someway I can format this into fixed
width, a comparison would be

string s = "21";
Console.WriteLine(s.PadLeft(4,'0'));

Result: 0021

Currently my result is as follows(the value of 15 changes because it is the
octet length

15 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I need it to look like

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

Any assistance would be greatly appreciated.

I'm afraid I'm still not entirely sure what you want to do - but
BinaryWriter and BitConverter can both help converting the length into
4 bytes.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
public class bind_transmitter : PDUHeader, IProtocolDataUnit
{
#region Public Ctor
public bind_transmitter()
{
this.command_id = CommandConstants.BIND_TRANSMITTER;
this.command_status = 0;
this.addr_npi = 0;
this.addr_ton = 0;
this.address_range = String.Empty;
this.interface_version = 0;
this.password = String.Empty;
this.system_id = String.Empty;
this.system_type = String.Empty;
}
#endregion
#region Private Properties

private byte _addr_npi;
private byte _addr_ton;
private string _address_range;
private byte _interface_version;
private string _password;
private string _system_id;
private string _system_type;

#endregion

#region IProtocolDataUnit Members


/// <summary>
/// Identifies the ESME system requesting to bind as a transmitter
with the SMSC
/// </summary>
public string system_id
{
get { return (_system_id); }
set { _system_id = value; }
}

/// <summary>
/// The password may be used by the SMSC to authenticate the ESME
requesting to bind.
/// </summary>
public string password
{
get { return (_password); }
set { _password = value; }
}

/// <summary>
/// Identifies the type of ESME system requesting to bind as a
transmitter with the SMSC.
/// </summary>
public string system_type
{
get { return (_system_type); }
set { _system_type = value; }
}

/// <summary>
/// Indicates the version of the SMPP protocol supported by the ESME.
/// </summary>
public byte interface_version
{
get { return (_interface_version); }
set { _interface_version = value; }
}

/// <summary>
/// Indicates Type of Number of the ESME address. If not known set
to NULL
/// </summary>
public byte addr_ton
{
get { return (_addr_ton); }
set { _addr_ton = value; }
}

/// <summary>
/// Numbering Plan Indicator for ESME address. If not known set to
NULL.
/// </summary>
public byte addr_npi
{
get { return (_addr_npi); }
set { _addr_npi = value; }
}

/// <summary>
/// The ESME address. If not known set to NULL.
/// </summary>
public string address_range
{
get { return (_address_range); }
set { _address_range = value; }
}

public byte[] Serialize()
{
MemoryStream ms = new MemoryStream();

using (BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(this.command_length);
bw.Write(this.command_id);
bw.Write(this.command_status);
bw.Write(this.sequence_number);
bw.Write(this.system_id);
bw.Write(this.password);
bw.Write(this.system_type);
}

byte[] buf = ms.ToArray();

uint len = (uint)buf.Length;
uint totalLen = len + (uint)len.ToString().Length;
buf[0] = (byte)totalLen;

return (buf);
}

}

Hi, thanks for the quick response, sorry I am not explaining myself properly

I have posted a snippet of the code, the byte array has the format as follows:

4 Octet Integer - command_length
4 Octet Integer - command_id
4 Octet Integer - command_status
4 Octet Integer - sequence_number
16 Octet String - system_id
9 Octet String - password
13 Octet String - system_type
1 Octet String - interface version
1 Octet Integer - addr_ton
1 Octet Integer - addr_npi
41 Octet String - address_range

[00 00 00 21][00 00 00 04][00 00 00 00][00 00 00 00]00 00 00 00 00 00 00 00
00
00 00 00 00 00 00 00 00

bw.Write(this.command_length); this results 21 i need it to
make it result in the following 00 00 00 21

I want to serialize(not the .NET Serialization) the above class into the
format of the byte array described above

Jon Skeet said:
Michael said:
I need to create a formatted byte array for SMPP, e.g.:

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

[00 00 00 21] is the length of the entire message in octets, however the
value is only in the 4 octet, is there someway I can format this into fixed
width, a comparison would be

string s = "21";
Console.WriteLine(s.PadLeft(4,'0'));

Result: 0021

Currently my result is as follows(the value of 15 changes because it is the
octet length of the entire structure

15 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00

I need it to look like

00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

Any assistance would be greatly appreciated.

I'm afraid I'm still not entirely sure what you want to do - but
BinaryWriter and BitConverter can both help converting the length into
4 bytes.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
bw.Write(this.command_length); this results 21 i need it to
make it result in the following 00 00 00 21

Unfortunately you haven't posted the declaration for command_length.
Is it of type byte by any chance? If so, cast it to an int.

I can never remember which way endianness works - if this does the
wrong thing, use my EndianBinaryWriter from http://pobox.com/~skeet/csharp/miscutil
which lets you specify the endianness.

Jon
 
Sorry here is the definition for command_id

using System;
using System.Collections.Generic;
using System.Text;

namespace M.SouthAfrica.SMPP.SMPPLibrary
{
public class PDUHeader
{
#region Private Properties
private uint _command_length;
private uint _command_id;
private uint _command_status;
private uint _sequence_number;

#endregion

/// <summary>
/// The command_length field defines the total octet length of the
SMPP PDU
/// packet including the lengthfield.
/// </summary>
public uint command_length
{
get { return (_command_length); }
set { _command_length = value; }
}

/// <summary>
/// The command_id field identifies the particular SMPP PDU.
/// </summary>
public uint command_id
{
get { return (_command_id); }
set { _command_id = value; }
}

/// <summary>
/// The command_status field indicates the success or failure of an
SMPP request.
/// It is relevant only in the SMPP response PDU and it must contain
a NULL value in an SMPP request PDU.
/// </summary>
public uint command_status
{
get { return (_command_status); }
set { _command_status = value; }
}

/// <summary>
/// This field contains a sequence number which allows SMPP request
and responses
/// to be associated for correlation purposes. The use of sequence
numbers for
/// message correlation allows SMPP PDUs to be exchanged
asynchronously.
/// Assignment of the sequence_number is the responsibility of the
SMPP PDU originator.
/// The sequence_number should be increased monotonically for each
submitted SMPP
/// request PDU and must be preserved in the associated SMPP
response PDU.
/// </summary>
public uint sequence_number
{
get { return (_sequence_number); }
set { _sequence_number = value; }
}
}
}
 
Sorry here is the definition for command_id

using System;
using System.Collections.Generic;
using System.Text;

namespace M.SouthAfrica.SMPP.SMPPLibrary
{
public class PDUHeader
{
#region Private Properties
private uint _command_length;
private uint _command_id;

In that case, calling BinaryWriter.Write(_command_id) will write four
bytes out.

If you believe it doesn't, please try to show a short but complete
program to demonstrate it, as per the link I gave you earlier. I
suspect you'll find that something else is going on - in particular, I
think the problem is that it's writing out 21 00 00 00 instead of 00
00 00 21. That would be fixed by using my EndianBinaryWriter.

Jon
 
Ok Thank You, I think I understand what you mean, I am unfortunately busy
with something else now setting up our DC, will get back to this later, Thank
you very much for your assistance, I will post back once I have fixed the
issue.
 
Back
Top