473,795 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(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 3570
Michael <Mi*****@discus sions.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.WriteLi ne(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.co m>
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_transmitte r : PDUHeader, IProtocolDataUn it
{
#region Public Ctor
public bind_transmitte r()
{
this.command_id = CommandConstant s.BIND_TRANSMIT TER;
this.command_st atus = 0;
this.addr_npi = 0;
this.addr_ton = 0;
this.address_ra nge = String.Empty;
this.interface_ version = 0;
this.password = String.Empty;
this.system_id = String.Empty;
this.system_typ e = String.Empty;
}
#endregion
#region Private Properties

private byte _addr_npi;
private byte _addr_ton;
private string _address_range;
private byte _interface_vers ion;
private string _password;
private string _system_id;
private string _system_type;

#endregion

#region IProtocolDataUn it 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_versi on
{
get { return (_interface_ver sion); }
set { _interface_vers ion = 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.c ommand_length);
bw.Write(this.c ommand_id);
bw.Write(this.c ommand_status);
bw.Write(this.s equence_number) ;
bw.Write(this.s ystem_id);
bw.Write(this.p assword);
bw.Write(this.s ystem_type);
}

byte[] buf = ms.ToArray();

uint len = (uint)buf.Lengt h;
uint totalLen = len + (uint)len.ToStr ing().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.c ommand_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*****@discus sions.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.WriteLi ne(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.co m>
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...@discus sions.microsoft .comwrote:

<snip>
bw.Write(this.c ommand_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 EndianBinaryWri ter 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.Collecti ons.Generic;
using System.Text;

namespace M.SouthAfrica.S MPP.SMPPLibrary
{
public class PDUHeader
{
#region Private Properties
private uint _command_length ;
private uint _command_id;
private uint _command_status ;
private uint _sequence_numbe r;

#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_lengt h); }
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_statu s); }
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_numb er); }
set { _sequence_numbe r = value; }
}
}
}

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

<snip>
bw.Write(this.c ommand_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 EndianBinaryWri ter 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...@discus sions.microsoft .comwrote:
Sorry here is the definition for command_id

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace M.SouthAfrica.S MPP.SMPPLibrary
{
public class PDUHeader
{
#region Private Properties
private uint _command_length ;
private uint _command_id;
In that case, calling BinaryWriter.Wr ite(_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 EndianBinaryWri ter.

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...@discus sions.microsoft .comwrote:
Sorry here is the definition for command_id

using System;
using System.Collecti ons.Generic;
using System.Text;

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

In that case, calling BinaryWriter.Wr ite(_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 EndianBinaryWri ter.

Jon

Jul 25 '07 #7

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

Similar topics

1
3528
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 not IE. Would someone please take a look at this and tell me if it can made to work in IE? If it can, a hint on the syntax/code would be much appreciated! PS, this would also get rid of my fixed background. Thanks in advance Jaz
179
44457
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
8
4045
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, columns 0-7 are an integer and columns 8-23 are a float. In _most_ files, the first few columns of the float are blank space to make it human readable. But not always. So, once they have been read from the file, I try to parse out the different...
5
8324
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 array manipulations (i'm leaving access to legacy code out of the scope of this message) on the other hand fixed statement does consume resources to execute, not to mention if "everything" is fixed, then dynamic object reallocation becomes...
8
5421
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 can do this with 2 substring calls, but it will need to rebuild the string just to write a few characters. Here is the simple, but inefficient, version: string s = "0123456789";
4
2205
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 { public byte MsgType; public uint Tag; public fixed byte ID; public fixed byte Val1;
0
1232
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) say. I added some stuff to fixedint.py to make
4
4004
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 row in a table etc... But does fixed-with mean that every column has a fixed with: for example first column is 10 char wide and second column is 30 char wide?
2
7208
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.