Created
June 26, 2016 17:15
-
-
Save jianminchen/fbea867f130b7d84e44085eab3671883 to your computer and use it in GitHub Desktop.
Get a char from a binary number with 4 bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* input is string of binary number, at most 4 bits "1100" | |
*/ | |
public static char getCharFromBinaryNumber(string s) | |
{ | |
int sum = 0; | |
foreach (char c in s) | |
{ | |
int value = (c - '0'); | |
sum = value + sum * 2; | |
} | |
if (sum >= 0 && sum <= 9) | |
return (char)(sum + '0'); | |
else | |
{ | |
string charStr = "ABCDEF"; | |
char[] charArr = charStr.ToCharArray(); | |
return charArr[sum - 10]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment