Jump to content
  • Advertisement
Sign in to follow this  

Assembly and C#

This topic is 3936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a string in Hex (very very very long) which needs to be converted into a byte array in C#. To do this takes around 2 minutes for 145KB (if error checking is disabled. If not, 5 minutes). What I would really like to do is use an assembly routine which will probably do it in a few seconds. What I would like to know is; is it possible to run an assembly routine from C# code? (The .COM file takes the string as input and returns the result) Thanks...

Share this post

Link to post
Share on other sites
I'm not sure, but you might be able to use P/Invoke.

Have a look at that!

Basically, it's a good way of including non-managed code into your C# from what I can remember.

Share this post

Link to post
Share on other sites
Look at this horror for machine code in C++/CLI:

delegate Int32 DoubleNum(Int32 x);
unsigned char pNative[] =
0x55, // push ebp
0x8B, 0xEC, // mov ebp,esp
0x8B, 0x45, 0x08, // mov eax,dword ptr [arg]
0x03, 0x45, 0x08, // add eax,dword ptr [arg]
0x5D, // pop ebp
0xC3 // ret
DoubleNum^ pDoubleNum = (DoubleNum^)
(IntPtr)pNative, DoubleNum::typeid);

Share this post

Link to post
Share on other sites
What you would really like to do is write an efficient routine in C#.

My computer isn't top-of-the-line, but it still manages to generate 145k bytes in less than 400 milliseconds without any special optimization effort on my part...

byte [] output = new byte[input.Length / 2];
byte a = 0;
int i = 0;

foreach (char c in input)
byte x = hex(c);

if (i % 2 == 0) a = x;
else output[i/2] = (byte)(a * 16 + x);


Where hex uses a straightforward switch statement to return the 0-15 value of an 0-9A-Fa-f character.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!