c#


What is the C# equivalent of the PHP Pack() function?


I have a line of PHP code that does the following:
$xml = "<xml><request>...[snipped for brevity]...</request></xml>";
$request = pack('N', (strlen($xml)+4)).$xml;
What this appears to do is prepend a binary string of the length of $xml (plus 4) to the value of $xml.
How do I do the equivalent of this in C#?
It looks like your request requires a length prefixed string made up of single byte characters, so it's gonna need a bit more work in C# which uses Unicode characters. I'm going to assume you want your string of XML encoded with UTF-8. We also won't be able to use a string to hold the request when its been put together, instead we'll use a byte array.
using System;
using System.IO;
using System.Text;
class Program
{
static void
Main(string[] args)
{
string xml = "<xml><request>...[snipped for brevity]...</request></xml>";
using ( MemoryStream stream = new MemoryStream() )
{
using ( BinaryWriter writer = new BinaryWriter(stream) )
{
byte [] encodedXml = Encoding.UTF8.GetBytes(xml);
writer.Write(ToBigEndian(encodedXml.Length + 4));
writer.Write(encodedXml);
}
byte [] request = stream.ToArray();
// now use request however you like
}
}
static byte []
ToBigEndian(int value)
{
byte [] retval = BitConverter.GetBytes(value);
if ( BitConverter.IsLittleEndian )
{
Array.Reverse(retval);
}
return retval;
}
}
The other thing to notice here is that the php pack() function with the 'N' parameter forces big-endian on your leading 4 bytes. I therefore wrote the ToBigEndian() method to handle conversion into big-endian on any platform.
System.IO.BinaryWriter would appear to be a closer match.
I'm not a PHP guru (haven't used it in years), but the following is probably pretty close in functionality:
using System.IO;
using System.Text;
string xml = "the original data to pack";
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms))
{
byte[] data = Encodings.ASCII.GetBytes(xml);
bw.Write((Int32)data.Length + 4); // Size of ASCII string + length (4 byte int)
bw.Write(data);
}
request = ms.ToArray();
}

Related Links

How can I get checkboxes not in datagrid selected based on database value Y and N?
Deserialize Single Dimension JSON Array C#
Can Outlook Notes(memo) add Item on VSTO(C#)?
Return data from self loopoing method in C#
Add row in GridControl Devexpress onClick
Store static values into .net app [closed]
Manipulate with data in list - C#
WPF Bind Selected TabItem Background Property Programmatically
ASP NET - delete row using dataset in SQL
Why does the cursor disappear on combobox DroppedDown in c#
Json from a get API Call C#
c# mvc 5, facebook login - How to ask extra user info after initial login
How do I deal with INSERT parameters which may be null, but are also converted?
Checkbox selection within gridview tossed after postback
XAML customize scrollbar in ListView (UWP)
Translate cURL request with Open SSL Authentication with .PEM file to .NET C#

Categories

HOME
http
redux
alasql
intellij-idea
formatting
intellisense
latex
opencv4android
reference
ebay-api
livecharts
azure-resource-manager
pandoc
query-string
snmp
confluence-rest-api
google-my-business-api
openbsd
mamp
solidity
hdf5
comma
smooth-scrolling
bootstrap-datetimepicker
turn.js
concatenation
struts
ithit-webdav-server
ios9
ng2-bootstrap
circle
epub
jsonapi-resources
dropdownbox
android-geofence
gstreamer-1.0
outlook-2010
queryover
word-wrap
stdin
gem-fury
mixed-models
rbac
file-descriptor
android-graphview
gradlew
encoder
photoshop-script
acceleo
timefield
stripes
distcc
jspx
networkstream
git-cherry-pick
ideavim
wordpress-plugin
gnu-sort
maven-antrun-plugin
om
roslyn-code-analysis
textblob
cakephp-2.6
genome
mergecursor
copy-on-write
parseexception
autopep8
frisby.js
illegalargumentexception
apache-pivot
go-gin
yosemite
famous-engine
dotcmis
rpt
satisfiability
nvcc
nomachine
nop
ie-developer-tools
aspnet-compiler
violation
xrandr
dynamic-expresso
leap-year
mosix
gmail-imap
django-tinymce
quit
activescaffold
iphone-maps
authkit
qlibrary

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App