masking
-
I have string of Hex values,so i should concatenate two hex values from the string and wanted to convert these Hex values to Binary format and after converting to binary, how should i mask the last three binary values?
srija wrote:
the last three binary values
are you talking about bits, bytes, or something else ? to convert from hexa representation to binary representation, it's easy. each hex character equals 4 bits :
CString strBinary;
switch (char cHex) {
case '0': strBinary = "0000"; break;
case '1': strBinary = "0001"; break;
case '2': strBinary = "0010"; break;
case '3': strBinary = "0011"; break;
case '4': strBinary = "0100"; break;
case '5': strBinary = "0101"; break;
case '6': strBinary = "0110"; break;
case '7': strBinary = "0111"; break;
case '8': strBinary = "1000"; break;
case '9': strBinary = "1001"; break;
case 'A': strBinary = "1010"; break;
case 'B': strBinary = "1011"; break;
case 'C': strBinary = "1100"; break;
case 'D': strBinary = "1101"; break;
case 'E': strBinary = "1110"; break;
case 'F': strBinary = "1111"; break;
} -
I have string of Hex values,so i should concatenate two hex values from the string and wanted to convert these Hex values to Binary format and after converting to binary, how should i mask the last three binary values?
I am not sure what you mean with masking, but if you like to extract a hexadecimal value from a string and want to set the last 3 bits to 0 you can do it like this:
#include <stdlib.h>
#include <stdio.h>int main(int argc, char **argv) {
char myHexString[] = "1A";
unsigned char myByte = 0x0;
sscanf(myHexString, "%X", &myByte);
myByte &= 0xF8; // set the last three bits of the byte to 0
printf("%d, 0x%x\n", myByte, myByte);
return 0;
}Hope that helps you out. Walter -- modified at 7:55 Thursday 2nd March, 2006
-
srija wrote:
the last three binary values
are you talking about bits, bytes, or something else ? to convert from hexa representation to binary representation, it's easy. each hex character equals 4 bits :
CString strBinary;
switch (char cHex) {
case '0': strBinary = "0000"; break;
case '1': strBinary = "0001"; break;
case '2': strBinary = "0010"; break;
case '3': strBinary = "0011"; break;
case '4': strBinary = "0100"; break;
case '5': strBinary = "0101"; break;
case '6': strBinary = "0110"; break;
case '7': strBinary = "0111"; break;
case '8': strBinary = "1000"; break;
case '9': strBinary = "1001"; break;
case 'A': strBinary = "1010"; break;
case 'B': strBinary = "1011"; break;
case 'C': strBinary = "1100"; break;
case 'D': strBinary = "1101"; break;
case 'E': strBinary = "1110"; break;
case 'F': strBinary = "1111"; break;
} -
I said about bits. I have array[6]={0x7D 0x0c 0x2f 0x65 0xA5 0x4B}... I should convert all Hex bytes to Binary format and then i should concatenate. for Eg:0001111111111111, now i should mask the last three bits i,e 000.
-
if only the 3 bits are important for you there, you don't need to make all the concatenation stuff... only get the last byte (here
0x4B
), cast it into anint
(char c = array[5]
) and mask it :c = c & 0x07
-
Yes, you are correct, just i used strcat(), it worked for me. now i got one more problem, how can i represent a binary value of 11100001110 into a decimal?
srija wrote:
...how can i represent a binary value of 11100001110 into a decimal?
Are you wanting to know how to represent a base-2 number as base-10 instead?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
srija wrote:
...how can i represent a binary value of 11100001110 into a decimal?
Are you wanting to know how to represent a base-2 number as base-10 instead?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
Can i use atoi function????plz check this?? s[20]=11100001110 int power = 1; int sum = 0; for (int i = l-1; i >= 0; l--) {sum += power * atoi(s[i]); power = power * 2; } printf("%d", sum)
-
srija wrote:
...how can i represent a binary value of 11100001110 into a decimal?
Are you wanting to know how to represent a base-2 number as base-10 instead?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Can i use atoi function????plz check this?? s[20]=11100001110 int power = 1; int sum = 0; for (int i = l-1; i >= 0; l--) {sum += power * atoi(s[i]); power = power * 2; } printf("%d", sum)
-
Can i use atoi function????plz check this?? s[20]=11100001110 int power = 1; int sum = 0; for (int i = l-1; i >= 0; l--) {sum += power * atoi(s[i]); power = power * 2; } printf("%d", sum)
Why go to all of this trouble when
strtoul()
will work just fine?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb