|
All errors are equal, but some errors are more equal than others...
(yes|no|maybe)*
|
|
|
|
|
s_mon wrote: All errors are equal, but some errors are more equal than others...
Seems I've heard that before. Maybe at that farm I used to work at?
|
|
|
|
|
public int CharToInt(char pcChar)
{
switch(pcChar)
{
case '0' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '1' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '2' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '3' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '4' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '5' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '6' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '7' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case '8' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
case 'A' :
return 10;
break;
case 'B' :
return 11;
break;
case 'C' :
return 12;
break;
case 'D' :
return 13;
break;
case 'E' :
return 14;
break;
case 'F' :
return 15;
break;
default :
return 0;
}
}
|
|
|
|
|
Please,
Show some mercy and give the coder this:
String Test = "1Fa";
int Answer = int.Parse(Test, NumberStyles.HexNumber);
Steve Wellens
|
|
|
|
|
C#? how about some good ol'
ans = c_val > '9' ? (10 + c_val - 'A') : c_val-'0';
|
|
|
|
|
What's he/she got against the number 9?
Regards
David R
|
|
|
|
|
riced wrote: What's he/she got against the number 9?
Oh, nothing. 9 will get flipped to 6 automagically
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Issue this guy a prescription for a book that covers basic things like this... Good God, is this another guy who thinks he gets a bonus for writing the most (inefficient) code?
|
|
|
|
|
return ( "0123456789ABCDEF".IndexOf ( pcChar ) ) ;
Though it still doesn't support lowercase letters.
|
|
|
|
|
that does not return the right default value though.
|
|
|
|
|
|
What about ... ?
return ( "0123456789ABCDEF".IndexOf ( pcChar.ToUpper ) ) ;
|
|
|
|
|
Never mind the verbose and unoptimized bulk, smell the uncessary (and complained-about-by-the-compiler)
case '0' :
return Convert.ToInt32(Convert.ToString(pcChar));
break;
I love the smell of crap code in the morning...
|
|
|
|
|
My real problem here is that the autor of the code didn't test EVERY possible character. After all, using default to return an useless and invalid value is not a good idea.
It will be much better to:
case 'G':
throw new Exception("G is an invalid value.");
.
.
.
case 'Z':
throw new Exception("Z is an invalid value.");
LOL
|
|
|
|
|
If you are going to be paid by the line, then you should continue this with lower case, punctuation and the other oddities available in the local character set...
It would have been far to easy too write
default:
{
throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
}
So we won't mention it.
Or at least I won't.
Oh bum.
modified on Sunday, April 19, 2009 5:44 AM
|
|
|
|
|
Except I just realized that to fit with his "coding style" I should have suggested
default:
{
throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
return pcChar;
break;
}
I feel better now.
Unclean, but better.
|
|
|
|
|
default:
{
throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
return pcChar;
break;
}
I must agree. This is a much better option. But I will keep the idea of checking each value, and ALSO use this pattern. So:
case 'G':
throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
return pcChar;
break;
case 'H':
throw new Exception(string.Format("\"{0}\" is an invalid value", pcChar));
return pcChar;
break;
As this is the original pattern from 0 to 8 (9 is NOT a number, LOL).
|
|
|
|
|
|
Why not check if the char equal 10? And since your getting payed by line go on till you reach the max value of an int64?
Cheers
|
|
|
|
|
I'm doing A LOT of such stuff nowadays. In assembly. (AT&T syntax)
# READ (char* bufer, int bufer_len)
# Reads a number from an ascii buffer and returns an actual number in eax
#
# PARAMETERS
# 1. Address to the buffer
# 2. The string's length
#
# Used registers:
# esi - contains all read characters
# ecx - index inside the buffer
# ebx - used in a conversion process char -> int
# edx - address to the buffer
# eax - the result
.globl read
.type read, @function
read:
.equ DIGIT_0,'0'
.equ DIGIT_9,'9'
.equ CASE_A,'A'
.equ CASE_F,'F'
.equ CASE_a,'a'
.equ CASE_f,'f'
.equ ONE_DIGIT_MASK, 0x0000000F
mov 8(%esp), %esi # save number of read chars
mov $0, %ecx
mov $0, %eax # there was the zero at the beginnig of the Universe
cmp %ecx, %esi # empty string case
je read_done
read_loop:
mov $0, %ebx
mov 4(%esp), %edx
#mov (%edx), %edx
mov (%edx, %ecx, 1), %bl # take the next char
cmp $DIGIT_9, %bl
jle read_0to9
read_AtoF:
cmp $CASE_a, %bl
jge read_AtoF_lowercase
sub $CASE_A, %bl # substitute'A'
jmp read_AtoF_continue
read_AtoF_lowercase:
sub $CASE_a, %bl # substitute 'A'
read_AtoF_continue:
add $10, %bl # add 10 to get the correct value
jmp read_char_done
read_0to9:
cmp $DIGIT_0, %bl
jl read_done
sub $DIGIT_0, %bl # substitute '0'
jmp read_char_done
read_char_done:
# (bl is a lower part of ebx)
add %ebx, %eax # ebx is a read-to-eat int
inc %ecx # increment the index
cmp %ecx, %esi # check if we're done
je read_done
shl $4, %eax # shift the number by one hex digit capacity.
jmp read_loop
read_done:
ret
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
What's with all the 'break' statements after 8? He didn't need them before? Or does he hope that Convert.ToInt32 does some voodoo that never returns to the switch block?
|
|
|
|
|
Brilliant! Aren't we glad English has only 26 letters? I'd like to see the Chinese version of the code (thousands of letters).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
User Class part :
Public Property SetChallengedCardList() As Integer
Set(ByVal value As Integer)
_challengedCardList.Add(value)
End Set
Get
Return 0
End Get
End Property
Public Property GetChallengedCardList() As ArrayList
Set(ByVal value As ArrayList)
_challengedCardList = Nothing
End Set
Get
Return _challengedCardList
End Get
End Property
and latter usage:
Dim u As New User()
u.SetChallengedCardList = 25
u.SetChallengedCardList = 27
u.SetChallengedCardList = 31
sstanko78
modified on Thursday, April 16, 2009 6:07 AM
|
|
|
|
|
|
Proper solution would be:
Private _challengedCardList As ArrayList
Public Sub AddItemToChallengedCardList(ByVal value As Integer)
_challengedCardList.Add(value)
End Sub
Public ReadOnly Property GetChallengedCardList() As ArrayList
Get
Return _challengedCardList
End Get
End Property
It was 21.00h in the office, so ....
sstanko78
|
|
|
|