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

Regex to capture text around a literal
Why my TempData is not working
Fetching information from a database
data type mismatch in criteria expression in c# 2010
Skip DataMember via DataContractSerializer
ServicedComponent Com proxy eventually fails
Embedded scripting on the AppStore - use Python, or snake my way in?
Using Expression to call a property and object and determine if the object is null or not
LINQ joining values from different classes
wp7 xna gestures not working
New window replicates
ASP.net C# Return JS Alert from another thread
Asp.net C# Webform forms Generator/Builder [closed]
Lambda event handler scope
Get OS-Version in WinRT Metro App C#
Enumerating DataPoints in view

Categories

HOME
youtube-api
kendo-ui
sip
service
openacc
webvr
glsl
arcgis
overrides
yuv
comments
coding-style
aws-sdk-cpp
kairosdb
watson-dialog
exchange-transport-agents
dat.gui
jquery-easyui
concatenation
handle
gulp-jshint
typeorm
jbossfuse
mobile-development
jboss-arquillian
foxpro
tikz
language-detection
traitsui
openvms
machine-code
gitosis
tunnel
blueimp
keychain
aura-framework
superclass
geminabox
openxava
apache-commons-cli
catalyst
directinput
hdr
foreground
scriptengine
android-progressbar
transport
personality-insights
sparkcore
log-analysis
sync
nspopupbutton
pyalgotrade
fastcgi-mono-server
boofcv
db2-connect
ekevent
mysql-error-1146
playing-cards
outlook-object-model
coinbase-php
aspmenu-control
wxperl
helicontech
flexbuilder
lightopenid
auto-compile
text-manipulation
weak-typing

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