C++ :: Streaming Bytes Into Integer

Sep 4, 2012

I have a FILE stream, and I want to create a function that streams a specified number of bytes (up to four bytes) and returns the value of this, as an integar.

For example, supposing that my FILE has the following data, in hex: 74 C8 02 33 F2 7B....... I then want to stream three bytes and store this as an integar. So, I want to stream "04 08 02". The data stored in the integar should then be "00 74 C8 02", because I have not streamed anything into the first byte. By converting the hex to dec, the integar should then be of the value 7653378 (if it is unsigned).

To try to achieve this, I have written the following function. I create an integar and initialise it to zero, then take each byte from the stream, and OR it with my integar. Then, I shift the integar left by 8, and read the next byte, and so on.

The problem is, when I convert "c" to "c_int", it adds on a load of 1's to the left of the "c" data. This then means that the OR comparison changes all those bits in my integar to 1.

How to solve this? I am also wondering whether there is a much more simple way of doing this, rather than having to write my own function....

Code:
int StreamFileToInt(FILE *fp, int num_bytes) {
char c;
int c_int;
int x = 0x0000;
for (int i = 0; i < num_bytes; i++) {

[Code] ....

View 4 Replies


ADVERTISEMENT

C/C++ :: Accommodate Double-size 8 Bytes In 4 Bytes Pointer In 32bit System?

Mar 15, 2015

how to accommodate double-size:8 bytes in 4 bytes pointer in 32bit system.

View 1 Replies View Related

C++ :: Why Address Of Object Is 6 Bytes And Not 8 Bytes On 64 Bit Linux

Mar 24, 2013

Code:
int i12 = 1001;
cout << i12 << " " << &i12 << endl;

gives the result: 0x7fff0d065098

It's 6 bytes, but I'd expect the address to be 8 bytes on my 64 bit machine. I am using Ubuntu 12.04, GNU compiler.

So, why the address is 6 bytes and not 8 bytes?

View 10 Replies View Related

C++ :: Radio Station - Streaming Audio On The Web

Jan 22, 2013

work for a radio station as their webmaster, and we have been looking into online streaming options for the last few months for our website. Pretty much everyone charges a monthly fee for this type of service and I feel like it is something I am capable of doing myself.

At the station we have a computer dedicated to operating the music/talk. On this computer there is a program that ques all the music, ads, ect.. This would be my audio source: Either the program playing all the music, or the computer itself.

The problem I have been pondering all night is how would I access this input source? The only audio input I am familiar with accessing is .mp3 or .wav files. How can I code my program to access this audio the program is producing and stream it to people on the web?

View 1 Replies View Related

C++ :: Streaming Input Through Two Constructors (two Classes - Using ADTs)

Jan 7, 2013

I have a main .cpp file which contains int main(int argc, char** argv) and 2 included personal headers which correspond to their linked implementation files.

What I am trying to do is use ifstream to pass integer values from a text file into my program, have the program execute, and then output those modified integers back into the same text file. I have tested the "program" portion of my project using declared values. I know that my program does what I have intended for it to do, but I can't seem to properly inject any outside data using files.

Here's what I have (not including all of the unrelated objective code):

Main.cpp

int main(int argc, char** argv) {
file File;
std::string gameFile = File.findFile(); // prompts user for existing file
if(gameFile.size() > 0) { // load saved game
std::ifstream in;
in.open(gameFile.c_str());

[Code] .....

In game.cpp, I am unsure of Player = new player; and Player = new player(data);. I have never tried doing this. This is simply my best guess of how to make this mess work properly. Hopefully I am close?

Game.h:

#ifndef _GAME_H_
#define _GAME_H_
#include "player.h"
class game {
std::string loadedGame;

[Code] .....

In game.h, I haven't yet tried anything like player* Player;. As stated in my comments above for game.cpp, I have no clue how to go about this.

player.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "player.h"
player::player() {
srand(time(0));

[Code] .....

In player.cpp, I really have no clue where to start with the issue. I'm not even exactly sure if any values are being passed to these variables. Though, I honestly haven't taken much time to problem solve. I don't want to waste a lot of time just to find out that my attempt is incorrect and/or unconventional.

player.h

#ifndef _PLAYER_H_
#define _PLAYER_H_
class player {
public:
player(); // default

[Code] ....

So really, my concerns are: Is my attempt mostly correct? If not, why?

View 1 Replies View Related

C++ :: WHILE Loop Error / In Streaming Data File (with Functions)

Nov 2, 2014

I have a working lab project with a loop error. Code posted in second post. Here's the requirements:

*
- program has to be able to handle file failure
- must loop (prefer to use a while loop here), read in the data, calculates values, etc. and then output the results.

*
The input data file includes

1. a 1 or a 0 indicating that there is a set of employee data following

2. the hourly rate of the employee

3. the humber of hours worked

4. the number of dependents

5. a 1 or a 0 indicating whether the employee is full time (1=full time).

Problem: At first I wouldn't loop back... so I moved the curly brace above the return0;

View 2 Replies View Related

C :: How To Split Int Into 2 Bytes

Oct 23, 2013

Im using some motors which run off PWM pins.Theres a High byte and Loq byte register (PWMH,PWML).I have an int which i need to put into these registers but i dont know how???so for example

int:84 -> PWMH=0x00 , PWML=0x54
int:310 -> PWMH=0x01 , PWML=0x36
int:11588 -> PWMH=0x2D , PWML=0x44

View 2 Replies View Related

C/C++ :: How Many Bytes / Bit In A String

Nov 2, 2012

How many bytes in a string ....

How many bit in a string ...

View 3 Replies View Related

C :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.

View 9 Replies View Related

C++ :: The Value Of 8 Bytes For Unsigned Integers

Jan 26, 2014

I'm confused about the actual value of 8 bytes for unsigned integers.

The below code suggests the value is 13217906525252912201:

#include <stdio.h>
#include <inttypes.h>
typedef uint64_t byte_int_t;
int main(void){
byte_int_t t;
printf("%" PRIu64 "
", t);
}

./runprogram
13217906525252912201

However, when I use a calculator, I get a different value: 2^64= 1.8446744e+19

So I was wondering is this really 8 bytes? So I try below test and it produces 8, as expected:

#include <stdio.h>
#include <inttypes.h>
typedef uint64_t byte_int_t;
int main(void) {
byte_int_t t;
printf("%u
", sizeof(t));
return 0;
}

So why does C and my calculator provide two different results?

View 1 Replies View Related

C++ :: Reading Bytes Without Bit Loss

Apr 13, 2013

I need to read repeatedly data from a MPEG2 file to the buffer of 188 bytes and analyse data bit by bit.

I have the problem with correct bytes reading from file. In my code listed below I have two methods for that.

First one is lossing this bytes which in hex_base mode have 0 at the begining, eg: 03, 0F, etc.

The second method based on read function which need to have buffer as a char (lenght > 1 byte). Because of that I receive different values from that from file in some cases.

How can I properly read such file?

Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main() {
fstream plik_in;

[Code] .....

View 2 Replies View Related

C++ :: Printing Out Bytes Of A File

Sep 4, 2012

I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.

This is being done by opening a pointer to the file with fopen, reading in each byte of data as a char, and then writing this char to the screen.

I have some image files (e.g. "image.jpg"), whose structure I know, so that I can test my program.

When I print out the chars, they are initially correct, and follow the structure of the file as expected.

However, after about 40 bytes, I find that every subsequent character is ' ' i.e. a blank character.

I then created a CharToBin function, which allows me to print out the actual bits in the char. When doing this, it shows that all the bits are 1 for the characters. i.e. most of the file is represented by 1's, which is clearly not correct.

This happens on all the image files I have tested, and furthermore, on several other non-image files. They all start printing out ' ' after a while. However, all these files are fine and not corrupted, e.g. the image files display correctly.

Code:
#include <fstream>
#include <iostream>
#include <sstring>
#include <string>
#include <stdio.h>
using namespace std;
string CharToBin(char ch) {
bool bits[8];
for (int i = 0; i < 8; i++)

[Code] ....

View 6 Replies View Related

C++ :: How To Convert Bytes To ASCII

Feb 7, 2014

How can I convert bytes to ASCII?, I read wikipedia about UTF-8 and I understood a little bit about add or split bytes to change the value.

Now I have those bytes

0xC7 0xE6 0xC2 0x91 0x93 0x7B 0xCE 0x01

And I found a program (DCode) that convert to 64 bits little-endian, supposedly those bytes in ASCII is this.

lun, 08 julio 2013 04:28:17 UTC <---

View 3 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related

C/C++ :: Converting 8-byte Integer To String Based Integer

Oct 15, 2014

Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.

Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}

I wrote:

#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';

[Code] ....

0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?

View 4 Replies View Related

C :: Checking How Many Bytes Are Remaining In A File?

Feb 22, 2013

while bytesremaining >=8 do something;
if bytesremaing <8 then something else .

I am new and don't know how to code for this.

View 2 Replies View Related

C :: How To Read In Nine Bytes And Check To See If There Is A Match

Feb 22, 2013

and see if the first three match a list then read in three more behind those that were left and perform a similar test on them and keep going?

View 8 Replies View Related

C :: Program To Convert Bits To Bytes

May 13, 2013

I made this program to convert bits to bytes, because I'm so sick of seeing ISP's advertise speeds in megabits, which I consider an intentional attempt to decieve :P And I think I've finally understood how the return value of scanf works since the last time I posted here, so my program can check to see if an integer was entered before processing the input, but I'm stuck on how to make the whole program start over if an integer is not entered. I have a hunch it would involve a loop, but I can't figure out how to make the program start over at "How many mb do you need converted?" if an integer is not entered into scanf..Here is the code I have so far:

Code:

#include <stdio.h>
int main () {
int b, mb, kb, Byte, kB, mB, gB;
char term;
}

[code]....

and my program makes the assumption for now at least, that mb will be inputted because that's the unit of measurement that i usually see advertised, and i didn't bother making an if statement to print a conversion in terms of gigabytes because i've never heard of a connection that fast :P

View 5 Replies View Related

C++ :: Breaking A Float Into Bytes For Binary I/O

Aug 12, 2013

I'd like to know how I could break a float into individual bytes for binary use.

Sounds simple, but it seems as though the compiler doesn't realize everything is just a series of bytes.

View 8 Replies View Related

C++ :: How To Create Character Array That Has 128 Bytes

Sep 23, 2014

m_sName-- character array shall be 128 bytes

How do i create a character array that has 128 bytes?

View 1 Replies View Related

C++ :: Deleting Certain Bytes From Binary File?

May 26, 2013

How to delete certain bytes from middle of a binary file?

Those bytes are in fact of a struct.

One approach I found was to re-write the whole data except the struct I want to delete. But may be its not good one if file size is in gb and I will have to write complete data again.

View 1 Replies View Related

C# :: How To Combine Two Bytes In Int32 To Get Original Value

Jan 6, 2015

how to combine two bytes (each byte is 8 bits wide) in order to get the original value. I am displaying values(from 0 to 500)sent from my microcontroller on the GUI.I can successfully display values from 0 to 255 as this requires just sending a byte. However sending values from 256 to 500 requires sending two bytes. The problem I am having is that I was unable to re-combine the received two bytes in order to get the original value. Below are my lines of code:

int main( void ) // this is the main function inside the microcontroller {
sei();
USI_TWI_Master_Initialise();
Spi_Master_Init();
while(1) // I am using this loop to send 500(111110100) {
Transmitt_Receive(244);//this function sends LOW BYTE of 500(11110100)
_delay_ms(1000);

[code]....

When I combined the two bytes using the above arrangements, instead of getting 500 my GUI displayed 62708.I got the same result when I used BitConverter.

value3 = BitConverter.ToInt32(val,0);

View 7 Replies View Related

C++ :: How To Read Single Bytes From Wifstream

Apr 27, 2013

I want to read a single byte from a wifstream. With an ifstream I would use read(), but given that the char type for an wifstream is wchar_t (2 bytes), this is also being used by read(), so how can I read a single byte from such a stream?

View 3 Replies View Related

C++ :: Write Process Memory Bytes

Apr 8, 2014

Writing bytes to external process to remove a detour that the program does

this is what im trying it fails

HANDLE Handle = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);

BYTE btLdrLoadDll[] = { 0x8B, 0xFF, 0x55, 0x8B, 0xEC };

if (!WriteProcessMemory(Handle, (BYTE*)((DWORD)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrLoadDll")), &btLdrLoadDll, sizeof(btLdrLoadDll), NULL)) {
CloseHandle(Handle);
std::cout << "
Failed to write Bytes to memory. Press enter to exit.";

[Code] ....

an also using the right address like this fails

WriteProcessMemory(Handle, (BYTE*)0x77C64F9F, &btLdrLoadDll, sizeof(btLdrLoadDll), NULL)

View 8 Replies View Related

C/C++ :: Reading Hexadecimal Bytes From A File?

Jan 19, 2014

#include <stdio.h>
#include <stdlib.h>
int main(int argc, int * argv) {
   char buffer[16];
   FILE *fp;
   unsigned long long test;
   unsigned char key[16];
   fp = fopen("D:key.txt","r");

[code].....

i searched for a code for reading bytes from a file and i found this one but when i run the program, it doesn't return the bytes written in the file, it returns another bytes / what is the problem?

View 3 Replies View Related

C/C++ :: Read Bytes From A Data File

Dec 5, 2014

I am a member of a group still developing the Microprose European Air War game. We have the source code and permission to do this. As the code dates back to 1998 I use Visual Studio 2006.

I have written many programs in Visual basic, and have the following one to read the airbase value in the "targets.dat" file. The file has a four byte header containing the number of tatgets, and a 32 byte data block containing the data for each target.

Private Sub Command1_Click()
List1.Clear
Dim nt As Long
Dim a4() As Long  
Open App.Path & " argets.dat" For Binary Access Read As 1
'read and display the number of targets  

[Code] ....

Despite many attempts I cannot write the C equivalent of this code into the eaw.exe source. All I really need to know is how to open the 'targets.dat' file and how to get the value of a byte (or integer, or long) at a given position as in the "Get #1, k, a4(n)" line.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved