Click here to Skip to main content
15,922,155 members
Home / Discussions / C#
   

C#

 
Questionwindows service username Pin
tomorrow_ft17-Sep-10 1:46
tomorrow_ft17-Sep-10 1:46 
AnswerRe: windows service username Pin
Calla17-Sep-10 2:09
Calla17-Sep-10 2:09 
AnswerRe: windows service username Pin
Ennis Ray Lynch, Jr.17-Sep-10 3:45
Ennis Ray Lynch, Jr.17-Sep-10 3:45 
AnswerRe: windows service username Pin
Mycroft Holmes17-Sep-10 4:58
professionalMycroft Holmes17-Sep-10 4:58 
QuestionOK, how would I do this in c# please Pin
stephen.darling16-Sep-10 12:34
stephen.darling16-Sep-10 12:34 
AnswerRe: OK, how would I do this in c# please Pin
harold aptroot16-Sep-10 12:40
harold aptroot16-Sep-10 12:40 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 12:46
stephen.darling16-Sep-10 12:46 
AnswerRe: OK, how would I do this in c# please [modified] Pin
Luc Pattyn16-Sep-10 13:24
sitebuilderLuc Pattyn16-Sep-10 13:24 
Here is an implementation, together with a series of tests.
I'm not claiming it is anywhere near the optimum, I mimicked your code for as much as I liked it.
Given full freedom I would return a byte array of variable length and never have to signal an overflow.

private static void runAllTests() {
	test("1");
	test("1000");
	test("1000000");
	test("1000000000");
	long j=1;
	for(long i=0; i<63; i++) {
		test(j.ToString());
		j+=j;
	}
	test("-1");
	test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
}

private static void test(string decimalDigits) {
	try {
		byte[] bytes=decodeSerial(decimalDigits, 10);
		string s=decimalDigits+" = ";
		foreach (byte b in bytes) s+=" "+((int)b).ToString("X2");
		log(s);
	} catch (Exception exc) {
		log(exc.ToString());
	}
}

// returns a little-endian byte array of given size, and representing the multi-precision
// integer value of a positive integer (entered as decimal string) assuming the size suffices.
// example: decodeSerial("1000", 4) returns E8 03 00 00 (4 bytes shown in hex).
private static byte[] decodeSerial(string decimalDigits, int numberOfBytes) {
	byte[] bytes=new byte[numberOfBytes];
	foreach (char c in decimalDigits) {
		int digit="0123456789".IndexOf(c);
		if (digit<0) throw new ArgumentException("bad char in decodeSerial("+decimalDigits+")");
		for (int m=0; m< numberOfBytes; m++) {
			digit+=bytes[m]*10;
			bytes[m]=(byte)(digit&0xFF);
			digit>>=8;
		}
		if (digit!=0) throw new OverflowException("decodeSerial("+decimalDigits+","+numberOfBytes+")");
	}
	return bytes;
}


FYI: log(string s) is any method that somehow outputs a string, could be as simple as Console.WriteLine(s);

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.


modified on Thursday, September 16, 2010 7:56 PM

GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 13:47
stephen.darling16-Sep-10 13:47 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 13:58
sitebuilderLuc Pattyn16-Sep-10 13:58 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 14:13
stephen.darling16-Sep-10 14:13 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 14:19
sitebuilderLuc Pattyn16-Sep-10 14:19 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 14:31
stephen.darling16-Sep-10 14:31 
GeneralRe: OK, how would I do this in c# please Pin
Luc Pattyn16-Sep-10 14:51
sitebuilderLuc Pattyn16-Sep-10 14:51 
GeneralRe: OK, how would I do this in c# please Pin
AspDotNetDev16-Sep-10 15:46
protectorAspDotNetDev16-Sep-10 15:46 
AnswerRe: OK, how would I do this in c# please Pin
PIEBALDconsult16-Sep-10 16:15
mvePIEBALDconsult16-Sep-10 16:15 
GeneralRe: OK, how would I do this in c# please Pin
stephen.darling16-Sep-10 16:50
stephen.darling16-Sep-10 16:50 
AnswerRe: OK, how would I do this in c# please Pin
David Skelly16-Sep-10 22:22
David Skelly16-Sep-10 22:22 
GeneralRe: OK, how would I do this in c# please Pin
AspDotNetDev17-Sep-10 10:14
protectorAspDotNetDev17-Sep-10 10:14 
Questionjust cant get this code to produce correct value Pin
stephen.darling16-Sep-10 10:08
stephen.darling16-Sep-10 10:08 
AnswerRe: just cant get this code to produce correct value Pin
Luc Pattyn16-Sep-10 10:24
sitebuilderLuc Pattyn16-Sep-10 10:24 
GeneralRe: just cant get this code to produce correct value Pin
stephen.darling16-Sep-10 10:43
stephen.darling16-Sep-10 10:43 
GeneralRe: just cant get this code to produce correct value Pin
Luc Pattyn16-Sep-10 10:50
sitebuilderLuc Pattyn16-Sep-10 10:50 
AnswerRe: just cant get this code to produce correct value Pin
T M Gray16-Sep-10 10:40
T M Gray16-Sep-10 10:40 
GeneralRe: just cant get this code to produce correct value Pin
stephen.darling16-Sep-10 10:58
stephen.darling16-Sep-10 10:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.