473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. unsigned Data = 0x378;
  2. short inpD7;
  3. char buffer1[32];
  4. inpD7 = DlPortReadPortUchar(Data);
  5. inpD7 = inpD7 & 0x80;            // isolate the first bit D7
  6. 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:

Expand|Select|Wrap|Line Numbers
  1. unsigned Status = 0x379;
  2. short inpStatus;
  3. char buffer2[32];
  4. inpStatus = DlPortReadPortUchar(Status);
  5. inpStatus = inpStatus | 0xF8;    // recall S7 is inverted 
  6. itoa (inpStatus,buffer2,2); 
  7.  
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.
Feb 4 '08 #1
1 2047
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.
Feb 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
10
by: Wrecked | last post by:
Could someone suggest a method to convert a Binary data to an ASCII data, with very less or no increase in the memory . The problem basically is there is an excrypted message which needs to be...
7
by: smith4894 | last post by:
Hello all, I'm working on writing my own streambuf classes (to use in my custom ostream/isteam classes that will handle reading/writing data to a mmap'd file). When reading from the mmap...
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
4
by: db2admin | last post by:
Hello, I want to plan rearranging tables in our database according to business areas. say all tables in business area A will be in seperate tablespace or tablespaces. I am planning to monitor...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.