473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Fixed Width Byte[] C#

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.


Jul 25 '07 #1
6 3544
Michael <Mi*****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 25 '07 #2
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 [C# MVP]" wrote:
Michael <Mi*****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 25 '07 #3
On Jul 25, 8:24 am, Michael <Mich...@discussions.microsoft.comwrote:

<snip>
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

Jul 25 '07 #4
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; }
}
}
}

"Jon Skeet [C# MVP]" wrote:
On Jul 25, 8:24 am, Michael <Mich...@discussions.microsoft.comwrote:

<snip>
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

Jul 25 '07 #5
On Jul 25, 8:46 am, Michael <Mich...@discussions.microsoft.comwrote:
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

Jul 25 '07 #6
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.

"Jon Skeet [C# MVP]" wrote:
On Jul 25, 8:46 am, Michael <Mich...@discussions.microsoft.comwrote:
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

Jul 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT...
179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
8
by: Brent Lievers | last post by:
Greetings, I have a question about parsing a fixed-width integer from a string. Lines of data are being read from a file having a very strict column-delimited format. In my example below,...
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
0
by: Andy Sy | last post by:
Hi Dan, I find that when doing bit-twiddling in pure Python, fixed-width integer support is an extremely handy capability to have in Python regardless of what the apologists (for its absence)...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.