Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

2010-08-30

Floating Point / Single to Hexadecimal (IEEE 754)

public string FloatToHex(float valueToConvert)
{
   byte[] byteArray = BitConverter.GetBytes(valueToConvert);
   Array.Reverse(byteArray);
 
   return BitConverter.ToString(byteArray).Replace("-""");
}

Output example: 12345.6 -> 4640E666

Solution came from VB.NET Hexadecimal to Floating Point / Single (IEEE 754)
What is IEEE 754 Floating-Point Arithmetic Standard?

2010-01-07

5 Very Useful C# Attributes

I hate 5 or 10 item lists, but this might be useful to someone:

2008-09-17

How to add spaces to ASP ListBox control

Problem:

When trying to indent some elements of the ListBox or DropDownList ASP controls with multiple spaces the browser only renders one space. Trying to add   instead of a space character renders the letters "&", "n", "b", "s", "p".

Solution:

Add Server.HtmlDecode(" ") instead of a simple space and everything should look fine.

private void FillList()
{
    listBox.Items.Add("Root");
    listBox.Items.Add(Server.HtmlDecode(" ") + "First level");
    listBox.Items.Add(Server.HtmlDecode("  ") + "Second level");
    listBox.Items.Add(Server.HtmlDecode("  ") + "Also second level");           
}

2008-01-31

How NOT to find out if a given string is integer or not in C Sharp


To find out if a given string is integer or not in C Sharp use the Int32.TryParse() method. Returns a boolean value: if it is true then the string is recognized as an integer number, if it returns false then the conversion did not succeed.
All the other numeric types have a TryParse() method.

Do NOT use the methods discovered by this guy because to throw an exception takes time. Also his character iteration method is not useful if we would like to parse a negative integer or a number that contains thousand separators. (He claims to be a Microsoft certified Team Leaded. For crying out loud... I mean come on...)

2007-12-17

Read a Stream into a String in C# .Net

using (MemoryStream ms = new MemoryStream())
{
//
// Write stuff into the memory stream with ms.Write();
//

// Jump to the start position of the stream
ms.Seek(0, SeekOrigin.Begin);

StreamReader rdr = new StreamReader(ms);
string str = rdr.ReadToEnd();
}