Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. masking

masking

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S srija

    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?

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    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;
    }

    S 1 Reply Last reply
    0
    • S srija

      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?

      W Offline
      W Offline
      walter76
      wrote on last edited by
      #3

      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

      1 Reply Last reply
      0
      • T toxcct

        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;
        }

        S Offline
        S Offline
        srija
        wrote on last edited by
        #4

        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.

        T 1 Reply Last reply
        0
        • S srija

          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.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #5

          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 an int (char c = array[5]) and mask it : c = c & 0x07

          S 1 Reply Last reply
          0
          • T toxcct

            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 an int (char c = array[5]) and mask it : c = c & 0x07

            S Offline
            S Offline
            srija
            wrote on last edited by
            #6

            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?

            D 1 Reply Last reply
            0
            • S srija

              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?

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              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

              C S 2 Replies Last reply
              0
              • D David Crow

                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

                C Offline
                C Offline
                chaitanya22
                wrote on last edited by
                #8

                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)

                S D 2 Replies Last reply
                0
                • D David Crow

                  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

                  S Offline
                  S Offline
                  srija
                  wrote on last edited by
                  #9

                  yes, i need it so... the value of 11100001110. i,e some 1806 or so...

                  1 Reply Last reply
                  0
                  • C chaitanya22

                    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)

                    S Offline
                    S Offline
                    srija
                    wrote on last edited by
                    #10

                    but atoi fuction si not working

                    1 Reply Last reply
                    0
                    • C chaitanya22

                      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)

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #11

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups