Dwords... more like FWords!
March 22, 2013 10:02 AM   Subscribe

Need some help converting an array of C# DWords to a PHP String....

My programming partner and I have been handed a set of DWords from the land of dotnet that we're using to build a key. It looks little like this:

tkKey.push_back(0x789d6152);
tkKey.push_back(0x630fe221);
tkKey.push_back(0x789d6152);
tkKey.push_back(0x630fe221);

... we want to convert it to a string that ends up looking like:
\x8f\xb3M\x05,
... etc...


How do we do that? I've been looking at pack and unpack, just finding myself horribly confused....
posted by ph00dz to Computers & Internet (5 answers total)
 
So you're using C# to create a PHP string? I'm not following how the numbers in your example correlate to the final string. Is it just a matter of escaping the concatenated numbers?
posted by artlung at 10:19 AM on March 22, 2013


Response by poster: We're trying to port a bit of functionality from C# to PHP.... essentially, trying to generate an encryption key.
posted by ph00dz at 10:22 AM on March 22, 2013


Best answer: If I'm reading your question right, pack is what you want. You can just use your dwords directly:

pack("V*", 0x789d6152, 0x630fe221, 0x789d6152, 0x630fe221)

And what you get is:

string(16) "Ra�x!cRa�x!c"

This string doesn't have the escapes in there like your example, but it's your data. Remember that when you pack little-endian data like this, your string is going to have each set of four bytes reversed. This is probably what you want, but if you want them the other way around, use "N*" instead of "V*".
posted by pocams at 10:50 AM on March 22, 2013


Response by poster: Thanks pocams!
posted by ph00dz at 12:39 PM on March 22, 2013


Response by poster: In case this comes up in anyone else's desperate search, here's something dumb to keep in mind: our array consists of dozens of those bytes. I had the values in quotes... you know... like one might expect. Don't do that... it won't work.
posted by ph00dz at 11:02 AM on March 23, 2013


« Older Best site for conducting employee background...   |   Web site that recommends songs from movies? Newer »
This thread is closed to new comments.