Rearranging Binary Data & Conversion to Hex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazygrey
    New Member
    • Nov 2007
    • 7

    Rearranging Binary Data & Conversion to Hex

    I'm using VC++ 2005 Express on Windows XP. I'm reading and writing parallel ports' pins. I'm using 10 outputs (D0-D6, C0,C2,C3) - a combination of data pins and control pins , and 6 inputs (D7, S3-S7). To accomplish that, I'm using dlportio.h to send/receive data and I have no problems with that. My problem is as follows:

    When I'm reading the port, dlportio.h allows to read the entire address (e.g. data address 0x378), and not the individual pins. Since I'm using the Data pins as outputs and inputs, I need to isolate D7 from the rest of the bits and that was done easily:

    Code:
    unsigned Data = 0x378;
    short inpD7;
    char buffer1[32];
    inpD7 = DlPortReadPortUchar(Data);
    inpD7 = inpD7 & 0x80;			// isolate the first bit D7
    itoa (inpD7,buffer1,2);		   // Convert int to string in binary format (base=2)
    Moreover, I have utilized S3-S7 since S2,S1&S0 are unused according to the parallel port documentations I found on line. I did this with the following code:

    Code:
    unsigned Status = 0x379;
    short inpStatus;
    char buffer2[32];
    inpStatus = DlPortReadPortUchar(Status);
    inpStatus = inpStatus | 0xF8;    // recall S7 is inverted 
    itoa (inpStatus,buffer2,2);
    I wanted to concinnate the inputs so I used strcat_s to do it and it worked nicely. The outputs results in 9 bits: D7 S7-S0, but I'm only interested in getting the first six bits so I need to truncate the last bits. How can I do that? Also, after that I need to convert it to Hex to complete the remainder of my code. I have tried using atoi to convert the string back to int with base 16, but it gave wrong results. Any help would appreciated. Sorry if this was a simple question but I'm stuck and searched the forum and I couldn't find what I'm looking for. Thanks in advance.
  • crazygrey
    New Member
    • Nov 2007
    • 7

    #2
    Actually I found a simple for my problem, I guess I didn't think about it before. I avoided using any characters and maintained everything in short int format. I used cout << hex << variable (easily changes the variable to its hex equivalent). Also, to eliminate the extra 0 bit I had, I divided inpStatus/2 and multiplied inpD7 by 2^7 to place it as the first bit. I added both results and it gave my hex/dec/bin combination. Very easy solution. Sorry to bother you guys.

    Comment

    Working...