Programming magic, glory, and juices.

C# Converting hexadecimal string to/from byte array, FAST!

June 26th, 2008


I previously wrote about how to quickly convert a byte array to its hex representation and back again in C. Now I’ve converted the same source code to C#. Even though there is a built in method using String.Format, it is too slow.

  public static string ByteArrayToHexString(byte[] Bytes)
  {
	 StringBuilder Result;
	 string HexAlphabet = “0123456789ABCDEF”;

	 Result = new StringBuilder();

	 foreach (byte B in Bytes)
		{
		Result.Append(HexAlphabet[(int)(B >> 4)]);
		Result.Append(HexAlphabet[(int)(B & 0xF)]);
		}

	 return Result.ToString();
  }

  public static byte[] HexStringToByteArray(string Hex)
  {
	 byte[] Bytes;
	 int ByteLength;
	 string HexValue = “\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9|||||||\xA\xB\xC\xD\xE\xF”;

	 ByteLength = Hex.Length / 2;
	 Bytes = new byte[ByteLength];

	 for (int x = 0, i = 0; i < Hex.Length; i += 2, x += 1)
		{
		Bytes[x]  = (byte)(HexValue[Char.ToUpper(Hex[i + 0]) - '0'] << 4);
		Bytes[x] |= (byte)(HexValue[Char.ToUpper(Hex[i + 1]) - '0']);
		}

	 return Bytes;
  }

How to program like a sketchy mofo

January 21st, 2008


To program like a sketchy ass mofo you must learn the basics.

Rule #1: Be shady when it comes to naming yo variables. Ain’t like you went to college or nuffin’.

char myNameIsWhat[maxNamage];
GetWindowTitle(cThrewWnd, myNameIsWhat, maxNamage);

Rule #2: Start each variable name with the first word all lowercase. It’ll ease yo eyes into it and help yo ass code even on crack.

if(yaReadinDis==yaAintHiEnuff)..

Rule #3: Don’t leave no room fo ya ho’s to escape. Lock em up tight. “lessWastedSpace=moRoomFoHoes”.

float laquishaWasLikeWoah;
if(laquishaWasLikeWoah<goldDigginHoe){
break(upWithLaquisha);}
else{
throw(yoHandsUpInDaAir);}

Rule #4: Be a ballar and use yo mad cryptic skills. Ight?

int aYa;
hoI_Is(aYa);

Rule #5: Reverse your constants in an if statement. You always gotta keep ‘em guessin’. Yo kno what I mean?

if(0==moniesInDaBankAccount){
goto robDaJewelryStore;}
else{
letItRide();}

Remember, it takes years of practice to purfect your sketcheousity. Compton wasn’t thugged out in a day. Ya heard?

PropertyGrid Performance

December 14th, 2007


Recently, even as recently as today, I was stretching my C# programming legs for work. We had an application that I’ve been maintaining that would update the user interface with statistics every so often. What I noticed was that as the updates occured, the user interface would become less responsive. I tracked down the issue to the fact that we were using the PropertyGrid incorrectly. What we had before was something along the lines of:

public void Event_OnUpdate(Class Sender)
{
   PropertyGrid_ObjectInfo.SelectedObject = new ObjectInfo(Sender);
}

Where ObjectInfo is basically a property wrapper for the Sender object. The slow responsiveness of the user interface was due to the fact that the PropertyGrid had to reload the SelectedObject each time it updated which, I guess is expensive. To solve the problem I simply used the following code instead.

public void Event_OnUpdate(Class Sender)
{
   if(PropertyGrid_ObjectInfo.SelectedObject == null)
      PropertyGrid_ObjectInfo.SelectedObject = new ObjectInfo(Sender);
   else
      PropertyGrid_ObjectInfo.Refresh();
}

I hadn’t seen anybody on Google run across the same problem with the PropertyGrid performing slowly so I decided to blog it. And that is that.

Certification: Web Applications in C# 70-315

July 5th, 2007


I made it through and am now MCAD certified. I wish they had a certification for C programming, but they only seem to have them for their .NET framework. I took the test this morning and scored a passing 810. The wording on the questions were the trickiest so far. I didn’t read the book for it cause I knew most of the information already and because I couldn’t stand to read another one of those books that are so full of filler that it is sick. Instead I just skimmed through the 800 page book and went over stuff I didn’t know. I did practice questions I found on the website I mentioned earlier and that helped the most. I am not sure if I will go for my MCSD. Now I have to wait 7 days for Microsoft to mail me my MCP ID and Access Code.

Certification: XML Web Services and Server Components in C# 70-320

July 2nd, 2007


Today, early this morning at around 11, I took my second exam for MCAD certification. I scored a 920 out of a possible 1000. I did a lot better on this test in part to BrainDumpCentral.com which is a website where people go and do brain dumps of the questions that they’ve had after they’ve taken the test themselves. A lot of the questions on BDC were almost exactly the same as on the real test. For this test I read the Microsoft certified book as I did for the last test. I didn’t bother with Transcender tests this time, as they didn’t really help me that much last time. There were a few things in there that I didn’t know about previously, such as .NET Remoting Objects, etc. I am now only one test away from getting the certification I need. Afterward I might get the MCSD (Microsoft Certified Solutions Developer) certificate, but I probably will take my time doing that. My incentive to get MCAD certified is a $1500 bonus my work is shelling out to the first two people who do.

Certification: Windows Applications in C# 70-316

June 28th, 2007


I took my 70-316 test yesterday. I scored a 780/1000. A 700 is required to pass. 780 isn’t that great, but then again I am not really .NET programmer, so I guess it is ok. I’ve done .NET windows and web applications and web services before but I don’t regularly program in .NET. Prior to taking the test, I read the whole Microsoft certified book for it. The book helped me gain a better understanding of the concepts and helped me learn the Microsoft terminology. But some of the questions on the test were not covered in the book. Other than that I used one Transcender Practice Exam and it was alright. I am not really looking forward to taking the other two tests, I kinda wish Microsoft would just award/give me the certification.

Certification

June 17th, 2007


Our work is giving us incentives to get our Microsoft certification. A company has to have a certain amount of individuals working for them with the these certifications in order to qualitify for Microsoft Certified Partner status and to receive discounted software and such. I plan on getting certified first as a Microsoft Certified Application Developer (MCAD). I will probably take the first of three tests next week sometime.

I never really believed in getting certified, but now I see its value. It is probably in my best interest anyways, since I never went to a four year college and only have an Associates degree in General Studies. Plus, our company is giving out bonuses to the first two people certified. Nice.