puneet's profileAchilles WatchesPhotosBlogListsMore ![]() | Help |
|
|
C# + Create a CSV Report
Heres the code to create a CSV Report in C#. Here reader is of type SqlDataReader. // Construct a comma-separated list all the column values in the current row
StringBuilder sb = new StringBuilder(); // to hold csv text file
// Create Header and sheet...
string quoter = @""""""; //
{
sb.Append( reader.GetName(j).ToString() ); // headings
sb.Append(","); // delimiter
}
sb.Append("\n");
// build the csv contents
string replVal = String.Empty;
while (reader.Read())
{
for(int k=0;k < reader.FieldCount;k++)
{
if(reader.GetValue(k).ToString()==null)
{
sb.Append("\"" + reader.GetValue(k).ToString() +" " + ",");
}
else
{
string temp = reader.GetValue(k).ToString();
replVal = temp.Replace("\"",quoter);
}
replVal+= " " +",";
sb.Append(replVal);
} //end if
sb.Append("\n"); // new row
} // end while
reader.Close();
reader = null;
Convert a HEX string to a string in C#This piece of code was originally got from Codeproject.com. This class will decode a HEX string to a string.
/// <summary>
/// Summary description for HexEncoding.
/// </summary>
public class HexEncoding
{
public HexEncoding()
{
//
// TODO: Add constructor logic here
//
}
public static int GetByteCount(string hexString)
{
int numHexChars = 0;
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
numHexChars++;
}
// if odd number of characters, discard last character
if (numHexChars % 2 != 0)
{
numHexChars--;
}
return numHexChars / 2; // 2 characters per byte
}
/// <summary>
/// Creates a byte array from the hexadecimal string. Each two characters are combined
/// to create one byte. First two hexadecimal characters become first byte in returned array.
/// Non-hexadecimal characters are ignored.
/// </summary>
/// <param name="hexString">string to convert to byte array</param>
/// <param name="discarded">number of characters in string ignored</param>
/// <returns>byte array, in the same left-to-right order as the hexString</returns>
public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i = 0; i < hexString.Length; i++)
{
c = hexString[i];
if (IsHexDigit(c))
newString += c;
else
discarded++;
}
// if odd number of characters, discard last character
if (newString.Length % 2 != 0)
{
discarded++;
newString = newString.Substring(0, newString.Length - 1);
}
int byteLength = newString.Length / 2;
byte[] bytes = new byte[byteLength];
string hex;
int j = 0;
for (int i = 0; i < bytes.Length; i++)
{
hex = new String(new Char[] { newString[j], newString[j + 1] });
bytes[i] = HexToByte(hex);
j = j + 2;
}
return bytes;
}
public static string ToString(byte[] bytes)
{
string hexString = "";
for (int i = 0; i < bytes.Length; i++)
{
hexString += bytes[i].ToString( "X2");
}
return hexString;
}
/// <summary>
/// Determines if given string is in proper hexadecimal string format
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static bool InHexFormat(string hexString)
{
bool hexFormat = true;
foreach (char digit in hexString)
{
if (!IsHexDigit(digit))
{
hexFormat = false;
break;
}
}
return hexFormat;
}
/// <summary>
/// Returns true is c is a hexadecimal digit (A-F, a-f, 0-9)
/// </summary>
/// <param name="c">Character to test</param>
/// <returns>true if hex digit, false if not</returns>
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c = Char.ToUpper(c);
numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
}
/// <summary>
/// Converts 1 or 2 character string into equivalant byte value
/// </summary>
/// <param name="hex">1 or 2 character string</param>
/// <returns>byte</returns>
private static byte HexToByte(string hex)
{
if (hex.Length > 2 || hex.Length <= 0)
throw new ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return newByte;
}
}
Enjai,
Puneet.H
Are the OS wars OVER??Apple announcement to use Intel chips in their Mac Boxes is stale but the recent developments are certainly not. There was a hack contest wherein 2 hackers tried to run Windows on the new Mac hardware. In the meantime Apple released a patch, which would enable users to run Windows.
This translates to a great deal of flexibility for users who don’t have to run Apple’s OS X. They can still have the stylish hardware of Mac and Windows to boot with it. This news is certainly a gamers delight. With this decision, Apple’s shares surged, as it would allow them to sell their hardware and OS to a larger section of the crowd who always want the best of both the worlds.
Following news of Vista being delayed to early 2007, and with this release from Apple it stands to gain a lot in the following months, as users who wanted to buy a PC in the holiday season would go and buy a Mac as they can still upgrade their operating system from Windows XP to Vista after its release.
Some are very unhappy about this, as the problems in the Windows OS will find way to their Mac’s. MS gains very little of the 3% of the PC market held by Mac’s. Dell and HP have their task cut out for them as they face new competition from Apple with hardware capable of running Windows. But then, this can certainly rewrite the OS war history. FireFox Foxed!! and Google OoglingWell, after a lot of arguments with a colleague of mine who had recently switched his browser to FireFox. I told myself to give it a shot again after switching to IE7 beta quite sometime back. So went ahead and installed the latest FireFox 1.5.0.1. with no extensions or plugins. I was greeted by a neat interface with tabbed browsing and the normal options in the menu. Although to prove my point, wanted to find BUGS in it. The back and forward buttons occupy too much space and the bookmarks toolbar takes a lot out of the viewing area. I hate using the scrollbar for anything and more so when i am viewing the page in fullscreen mode, which is always the case. And remembers passwords and private information until turned off and keeps prompting you for the silliest thing possible. Anyway, one of the greatest downsides is that I have installed macromedia player separately and IE recognizes it and displays the ads but FireFox will prompt you for installing the MISSING PLUGINS. I also couldn't use Ctrl+U shortcut for underlining in this blog, as it would show me the html source of the page. Also to increase viewing area, I had to customize the Navigation bar to display only icons and no text and unselect the Bookmarks bar. There's no option to take the menu out of the viewing area. Holy Crap, I have to use the mouse while i am using FireFox(FF). The only heartening thing about FF is just its a little light on memory and has some cool looking skins to it which obviously have to be downloaded. But the biggest blunders they ever created was the agreement with Google to integrate their search engine with it by providing a textbox. Because, i use MSN search as my first option and then turn to Google if i wanted something else. There's a option to add search engines to the browser. I added MSN and Wikipedia and was trying hard to delete the EBay, and other options, which is provided default along with the browser. There's no option to make a search provider as Default, but only Google!! Ok, here goes nothing, i had to INSTALL one more hack to delete the existing search provider options and after that i couldn't make MSN search my default option. So the only way is to delete Google from the list which i did. And the greatest idiotic move Mozilla and Google did, there is no way to add Google to the search provider list once it is gone. Because the page from which you add other search options they don't have Google!! This is a case of sheer audacity on the part of Google that competition are far back and they can take time off to churn Web1.0 products for a generation of Morons who use things without reading the Terms and Conditions. Keep dreaming. For more of Google's Facts check this link. And spread the word around. As of now, i will be using FireFox until there's a fix released for the latest bug found in IE!! MSN Search Kicks Butt and Getting Better at itBefore you guys think that i am one of those Microsoft fanatics and Google haters Club President. I should tell that i am a IP creation fanatic. There's nothing better than letting your creativity loose and giving birth to an Idea which in your mind will change the World. That's exactly what Microsoft have done with their Search Engine.
Sat, Oct 22, 2005 a very well known MS fanatic Mr.Scobleizer posted an hour long video on Channel 9. It featured an interview with Andy Edmonds, lead program manager, and Erik Selberg two of the geeks who work on making MSN Search better. Well i am talking about the video now because India just entered an arena boasting of broadband with 256Kbps sometime back so that i could download the 213Mb video!
Andy and Erik reply to Scoble's queries with oozing confidence and display the new features of MSN Search. MSN's engine is run by a Neural Network under the hood. That is the engine itself is a living creature as Andy puts it! He also gives an example of a search engine which had learnt to play a game on Web by itself.
It uses the decisions made by the user to get better. They also talk about the configurations of their datacentre's(the place where they store the part of the web downloaded). They run on Dual Core,Windows 64 bit OS with oodles of RAM and oodles of Hard Disk space as Andy discloses. This when compared to Googles Datacentre's which run on P3 machines because they consume far lesser power. Power Consumption is supposedly a major part of the expense in maintaining them.
Erik talks about the problems encountered while measuring RELEVANCY of a page, like Page A has link to Page B which has a link back to Page A and website mirrors. They talk about the competition Yahoo and Google in terms of standards met and index size(how much of the web has been sorted and downloaded into their datacentre's).
On the Standards part, MSN search is the best compliant engine and Yahoo search comes a close second with Google failing miserably. And on the Index size, Yahoo and Google maintain a whopping 20 Billion plus pages in their index, while MSN maintains a meagre 5 billion pages in its index. Does this necesarily mean that Yahoo and Google are way ahead of their game, I am afraid not. MSN Search has been consistently yeilding better results than Google. Yahoo,on the other hand recently agreed that they didnot want the No.1 spot in the search engine market and didnot want to compete with Google while Yahoo developers strongly disagreed to this statement by Yahoo's CEO!
The features include a whole new set of controls over variables in the SearchBuilder tab which searches with Freshness, Popularity and Exactness of the results for a given query. There is already the Windows Live Search which has already created a buzz. The only amusing fact is the promotion made by MSN Search, which is way too less. If Google Mars can get headlines on World News then MSN Search should have done way better.
The video is available at http://channel9.msdn.com/Showpost.aspx?postid=128677 . Just make sure you have a download accelerator before going on this adventure. Enjoy.
War between Capitalistic Enterprises and ALL wanting UsersWell before you people read this and give it a second thought, here is the Disclaimer:
The Views presented here are strictly personal.PERIOD.
I have kept track of the duel between Apple and DVD Jon from the day it started. Jon Lech Johansen earned the name DVD Jon when he cracked the DVD code in late 1999. In August 2004 he made headlines again when he published the key that Apple's wireless hi-fi bridge, Airport Express, uses to protect music streams.
This guy hacked into Apple iTunes 3 times
This is just one of them. He was hired by Apple for sometime and also did hack GOOGLE.
In a statement, Google confirmed Johansen's Video Viewer tweak. The company noted that the code was not a hack and did not in any way compromise Google's video viewer.
But anyway this is one man story.Surprisingly the development of the new DVD format BluRay has recieved serious flak as it has several security features which prevent the user from copying them or ripping them and has no backward compatibility with HDDVD.This is a feature which could save billions to the music and movie industry who have gone ahead with this but hasnt gone every well with the BIGGIES of home-pc entertainment wherein MS come into the picture. They want the user to be able to make copies of media so that he/she can play the content on their pc,music player...
Surprisingly recent studies found people would buy legitimate copies of music if it wasn't exhorbitantly priced. You guys can download software from sites which block playing of media without copyrights. But also the user would llike to have the freedom of playing the same everywhere be it cellphone, MP3 player,DVD player etc..
This accompanied with a strong conviction by the user to stay away from pirated content could do the world a lot of good
Puneet.H
|
|
|