Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created June 26, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/fbea867f130b7d84e44085eab3671883 to your computer and use it in GitHub Desktop.
Save jianminchen/fbea867f130b7d84e44085eab3671883 to your computer and use it in GitHub Desktop.
Get a char from a binary number with 4 bits
/*
* 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