473,386 Members | 1,867 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,386 software developers and data experts.

Converting Bytes into integer sets

hello, i'm writing a function that translate Bytes of type unsigned char to integer array of 1's and 0's, i'm taking a message of 33 Bytes and translating them in an int array of 264 integers, here's the function:
Expand|Select|Wrap|Line Numbers
  1. void readByte(unsigned char * buffer, int binarr[])
  2. {
  3.     unsigned char mask = 1 << 7;
  4.     bool mybit;
  5.     int j =0;
  6.     for ( int i =1; i <= sizeof(binarr) ; i++)
  7.     {
  8.         mybit = (bool) (buffer[j] & mask);
  9.         if (mybit) 
  10.             binarr[i]  = 1;
  11.         else 
  12.             binarr[i] = 0;
  13.         buffer[j]<<=1 ;
  14.         if ( i % 8 == 0)
  15.             j++ ;
  16.     }
  17. }
but it looks like something is wrong it cause when i try to display my array first it shows me much more than 264 integers and second its not binary it contains other numbers then 1's and 0's
here is my main:
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.     ifstream InFile("Myfile", ios::binary);
  4.     unsigned char message[33];
  5.     int bin[264];
  6.     if (!InFile)
  7.     { 
  8.         cerr<<"InFile could not be opened";
  9.         exit(1);
  10.     }
  11.     InFile.read( (char *)message, 33);
  12.     cout<<"my message is:";
  13.     for ( int i =0; i< sizeof(message) ; i++)
  14.         cout<<message[i];
  15.     readByte(message, bin);
  16.     cout<<"\n"<<"my binary representation is:";
  17.     for ( int i =0; i< 264 ; i++)
  18.         cout<<bin[i];
  19. }
Please help on this issue, if u need furthe explanation i'll be happy to answer
Oct 29 '07 #1
1 1783
weaknessforcats
9,208 Expert Mod 8TB
You might try an easier approach.

To see the bit in position 128 you:

quotient = your number / 128

This will be some number

Next:

result = quotient % 2

and the result is 0 or 1 as an integer

Or covert it to a displayable character:

charresult = result + '0'

In one line of code this is:

(your number / 128) % 2 + '0'

If you use a loop divide the 128 by 2 to get the next least significant bit.
Oct 29 '07 #2

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

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
3
by: =?Utf-8?B?Q2luZHk=?= | last post by:
Hi all, I am trying to connect to a electromyograph machine for my thesis project, and I have a VB DDE client whose DDE functionality I want to convert to VC++. However I can't make my VC++...
5
by: cmdolcet69 | last post by:
I have a program that saves values in the following format: 4096,4095,4044,3900,3812,........... to a text file. I need to know how to convert the 4096 into a HEX value in this format: 0x40 +...
15
by: itdevries | last post by:
Hi, I'm trying to read some binary data from a file, I've read a few bytes of the data into a char array with ifstream. Now I know that the first 4 bytes in the char array represent an integer....
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.