CharToInt
-
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; } }
-
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
-
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; } }
-
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; } }
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?
-
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; } }
return ( "0123456789ABCDEF".IndexOf ( pcChar ) ) ;
:-D Though it still doesn't support lowercase letters. -
return ( "0123456789ABCDEF".IndexOf ( pcChar ) ) ;
:-D Though it still doesn't support lowercase letters.that does not return the right default value though. :(
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
that does not return the right default value though. :(
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
It returns a better one.
-
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; } }
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...
-
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
-
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
-
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
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.
-
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; } }
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, 0x0000000Fmov 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, %ebxmov 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_doneread_0to9:
cmp $DIGIT_0, %bl
jl read_done
sub $DIGIT_0, %bl # substitute '0'
jmp read_char_doneread_char_done:
# (bl is a lower part of ebx)
add %ebx, %eax # ebx is a read-to-eat intinc %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:omg:
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.
-
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).
-
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).
-
return ( "0123456789ABCDEF".IndexOf ( pcChar ) ) ;
:-D Though it still doesn't support lowercase letters.What about ... ? return ( "0123456789ABCDEF".IndexOf ( pcChar.ToUpper ) ) ;
My little forums: http://code.bn2vs.com 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
-
Paulo Zemek wrote:
As this is the original pattern from 0 to 8 (9 is NOT a number, LOL)
I though 6 was not a number http://www.youtube.com/watch?v=29JewlGsYxs[^] :-D
Regards David R
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? :D Cheers
My little forums: http://code.bn2vs.com 70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
-
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[^]
-
Please, Show some mercy and give the coder this:
String Test = "1Fa";
int Answer = int.Parse(Test, NumberStyles.HexNumber);Steve Wellens
-
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; } }
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?
-
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; } }
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