C++ :: Convert Ifstream To Binary

Sep 5, 2013

How do I convert ifstream to binary and display the binary at the end. I have a file that when it contains numbers it can do it but when reading strings it has trouble. It acts as if there is no binary in the file.

View 17 Replies


ADVERTISEMENT

C++ :: Convert From Decimal To Binary With Base 2

Dec 7, 2014

I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);

[Code] .....

I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);

[Code] ......

View 5 Replies View Related

C :: Convert Binary Numbers To Decimal

Mar 27, 2013

The code should convert binary numbers to decimal but it doesn't..

Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{

[Code] ....

View 4 Replies View Related

C :: Convert Number To Binary From And Base Up To 10

Dec 7, 2014

I had an exercise that required me to convert a number to binary (base 2) which as simple enough.

Code:
#include <iostream>#include <iomanip>
#include <cmath>

using namespace std;
void Conversion (int n);
int main () {

[Code] .....

I now have a follow on exercise that requires me to convert to binary from ant base up to 10, i thought this would just be replacing the 2 with a variable obtained form the user, but i am having problems as within the function i am getting an error that i haven't passed enough arguments and i cant see why i get this. I did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);

[Code] ....

View 2 Replies View Related

C :: How To Convert Binary Table To Hexadecimal Value

Oct 2, 2013

i need to code a function that converts an array of 64 bits into a hexadecimal value, the one is tested gives me correct value except for the last hexadecimal letter.

Code:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <cstdlib>
#include <string>
typedef unsigned __int64 Ebyte;
#define length 64

[Code] .....

View 12 Replies View Related

C :: Convert Decimal To Binary On Language

Nov 15, 2013

The goal of my program is to convert a decmial number to a binary number.First, the program gets an input to an array of chars, and the function translate_dec_bin converts this array to a decimal number through the strtoul function.The problem is that my program prints the binary number with an additional "0".For exmaple, for the input 1 - the program prints 01 instead of 1, for the input 3 - the program prints 011 instead of 11.

Code:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 20
void translate_dec_bin(char s[]){
char st[sizeof(unsigned)*20] = { 0 };
}

[code]....

View 2 Replies View Related

C++ :: Convert Each Of Elements Of Sum Array Into Binary

Nov 9, 2014

Platform : c++, compiler: GNU gcc

I am new to programming and have written the code for the following program.

PROGRAM: Input 2 arrays => arrays 1 and 2 from the user each containing 5 elements. Sum is another array which is sum of elements of array 1 and array 2. Convert each of the elements of the sum array into binary. Count number of 1's in each binary element and output it to another array "arr".

Example: arr1 = {1,2,3,4,5}
arr2 = {6,7,8,9,10}
sum = {7,9,11,13,15}
binary of 7 [111], 9[1001], 11[1011], 13[1101], 15[1111]
No of 1's in 7 [3], 9 [2], 11 [3], 13 [3], 15 [4]
arr array will be {3, 2, 3, 3, 4}

I am not getting the desired output. My code is:-

include <iostream>
using namespace std;
int main() {
int array1[5];

[code].....

View 2 Replies View Related

C++ :: Function To Convert Decimal To Binary

Jul 11, 2014

What is the difference between the two functions below? I created the function in the top and my friend created the function in the bottom. I just want to know why the function with the while loop prints the binary numbers backwards. And the recursive function prints the binary numbers correctly.

void findBinary(int num) {
int remainder = 0;
while ( num > 0) {
remainder = num % 2;
cout << remainder;
num = num / 2;

[Code] .....

View 3 Replies View Related

C++ :: How To Convert Array To Balance Binary Tree

Jul 2, 2014

So if I have an array how do I convert it to a balance binary tree?

View 1 Replies View Related

C :: Convert Decimal Number To Binary And Store It In A String

Apr 16, 2014

I want to convert a decimal number to binary and store it in a string so that I can use that binary output for later computation. Here is what I did

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()

[Code] ....

​Now as you can see that all the binary output is in a[] but how do I get it into a string so that I can use something like printf("%s",string) and get the binary output ?

View 7 Replies View Related

C :: Use Main Function To Ask For Input To Convert Hexadecimal To Binary

Mar 15, 2013

Code:

#include<stdio.h>#define MAX 1000
int main(){
char binaryNumber[MAX],hexaDecimal[MAX];
long int i=0;
printf("Enter any hexadecimal number: ");
scanf("%s",hexaDecimal);

[Code]...

So this is my current code, is there anyway I can reduce the size and use a main function to ask for input and a call function to do all the conversion and return it? I am confused for the past few days trying to figure it out and finally ended up here. Anyway can I write it as a something like this

Code:

int main()
{
//ask for user input hexadecimal into here and call a let's say hex2binary() function
}

int hex2binary(...)
{
//an array with dynamic memory, malloc? and convert it and return values
}

I don't really need the full code, just a simple instruction on how and where to start.

View 2 Replies View Related

C :: Pass Decimal Number To Function And Convert It To Binary?

Feb 26, 2013

I'm trying to pass a decimal number to a function and convert it to binary and return it and print it out in main. But it prints out 1011 and then seg faults...not sure where it's tripping up

Code: int main(){
char* binNum = decToBin(25);
int i = 0;
while(binNum != NULL){

[Code].....

View 6 Replies View Related

C++ :: How To Convert Text File Into Binary And Vice Versa

Dec 25, 2013

The problem is that I want to write a C++ program that converts an ordinary text file into binary and then reads that binary file and converts it to text file so that this text file equals to first text file. I have wrote this code for it.

int main() {
string name1 = "first", name2 = "sec", name3 = "third";
int j = 0, k = 0;
ifstream ifs(name1.c_str()); // Here I want to read from the ordinary text file (name1).

[Code] .....

Now what the ofs.write(as_bytes(j), sizeof(int)); or ifs.read(as_bytes(k), sizeof(int)); exactly mean?

In practice, the file name1 contains digit 5 and its size is 1 byte. The name2 contains some character/sign like [] and its size is 4 bytes and name3 contains digit 0 and its size is 1 byte, why? I before have created the name1 file in ordinary text file mode.

My machine is Windows 7 32-bit. My compiler is MVS 2012.

View 9 Replies View Related

C++ :: How To Convert Ordinary Text File Into Binary And Vice Versa

Dec 23, 2013

How to convert an ordinary text file into binary and how to convert that binary file back to a text file so that the first text file equals with the last text file?

View 8 Replies View Related

C++ :: Convert List Of Binary Numbers From A File Into Decimal And Print To Screen

Sep 23, 2014

I am trying to make a program that will convert a list of binary numbers from a file into decimal and print the decimal to the screen. I have no problem doing the conversion, the problem comes up when our teacher wants the input file in a format as such:

3
10110101
11111111
10101010

The first number is supposed to tell the program how many different 8bit strings it is going to have to convert, and then the following lines are those binary numbers.

I am not very experienced with file inputs, and I know how to open files and read lines in.. The problem is, how to say "ok the first line says 3, so now I have to convert the next 3 lines" . I am assuming it is just a simple loop that I am missing....

View 4 Replies View Related

C++ :: Convert Binary To Base10 - Array Initialization Skipping Straight To Int Main?

Mar 3, 2013

I am attempting to write a program that converts binary to base10, and vice versa.

But in the function for converting Base10 to Binary, just as it reaches the line of code

int* binary = new int [a];
it skips straight to the int main()

All I'm attempting to do with that line of code is initialize the variable "a" into the elements of the array "binary".

[URL] ....

View 5 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

Dec 26, 2013

I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...

i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....

------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";

[Code] ....

When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.

The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.

I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.

When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..

input = input / BASE; I get C9 where I expected to get 48.

My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?

View 6 Replies View Related

C++ :: Convert 8-digit Binary Number To Decimal Number?

Jul 6, 2013

Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.

Sample Output 1:

8-bit Binary Number => 11111111
Decimal Number = 255

Sample Output 2:

8-bit Binary Number => 10101010
Decimal Number = 170

Sample Output 3:

8-bit Binary Number => 101010102
Number entered is not a binary number

#include <iostream>
using namespace std;
int main()
{
int num;

[code]....

View 2 Replies View Related

C++ :: Convert Binary Search With Int Array To Use String Array

May 8, 2014

I'm having trouble converting this binary search with an int array to use a string array..

Code:

#include <iostream>
#include <string>
using namespace std;
// Function prototype
int binarySearch(string [], int);

[Code] .....

View 3 Replies View Related

C++ ::  Going To A Specific Line Through Ifstream

Jan 12, 2015

I want to access specific lines in a "*.txt" file. I've heard of seekp() and what not, but don't know how to call them as such.

View 6 Replies View Related

C++ :: Ifstream Not Reading Next Line?

Aug 3, 2012

I have a file blah.txt like this:

3 1 4
1 2 3

and here is my code to read in that file:

Code:
int x = 0, y = 0, z = 0;
ifstream data("blah.txt");
while(x != 1)
{
data >> x >> y >> z;
}

For some reason, it just keeps readin in the values: 3 1 4

View 5 Replies View Related

C++ :: Passing Ifstream Object Between Functions

Mar 29, 2013

Code:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);

[Code] ......

I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.

View 9 Replies View Related

C++ :: Using Ifstream To Open Multiple Files

Apr 28, 2014

I am currently working on a C++ program for school. I am actually not finding too much difficulty in constructing the functions, enum-types, arrays and structs, however, I am finding great difficulty in using on ifstream variable to open multiple files.

I have posted the entire code that I have so far (even though I have pinpointed the issue to not properly opening the second file in ifstream).

I spent a couple of hours getting rid of certain functions/procedures, loops and variables and I get the same output (if what I removed doesnt crash it). I also get the same output whether I "open" the second file or not (meaning I removed all of the code for it and got the same output).

Here is the code (it's not finished because I am stuck on this file issue). It's a bit messy since I am now in debug mode versus program mode:

View 8 Replies View Related

C++ :: Ifstream Input To Different File Types

Mar 14, 2014

I want to create a function with a return type. However, I want to use it to read from a file (ifstream) and produce multiple different types of return types. The different file types returned would be always in the same order. For example

Text File:
Name
1
12
30
Area

I want to ifstream line 1 (Name) to an array of characters.
line 2, 3, and 4 to integers.
and line 5 as a string.

The basic problem is that if I make a function with one return type, it would only return one type of data to my int main(). I suppose I could create multiple functions that would run this for different variable types and destroy the invalid types. But this seems inefficient. It is being used to load in data from a previously saved file.

So far..

//function to load a player's data
string loaded(string fileName) {
ifstream loadfile;
loadfile.open(fileName);

[Code] .....

I was thinking I could possibly do something with a counter to count the lines and assign a value based on their order. The problem is with the 1 return value of a function. Maybe there is another operation I could use?

View 4 Replies View Related

C++ :: Ifstream Doesn't Read From The File

Aug 14, 2014

I wrote a program which was supposed to decrypt a file encrypted with the XECryption algorithm. Now, I know the decryption algorithm, but I have a problem with my ifstream object. It doesn't read anything at all, and the ofstream object just outputs a single random byte in a never ending loop. I've tried using cin, which works correctly, but it's not what I want.

Here's the code:

#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("file.in", ios::in);
if (!in.is_open())

[Code]....

I'm doing this on a Windows 8(.1) pc with Code::Blocks 13.12. In the file (file.in) I have replaced the points with spaces. What is wrong with the code?

View 9 Replies View Related

C++ :: Ifstream Selectively Read In Information?

Nov 13, 2013

If I have a data file.dat like this, for example:

...
other information
...
physics:
4,5,6,7
...

line:
(4,2), (2,4)

line:
(1,2), (2,3)

...
...

I want to design a class and corresponding code so that every time when it reads "line:" for the file.dat , it will push_back a new line into the line_t vector, and each time when it encounter physics it will put the values to physics, How can I implement this?

every data in file.dat is useful, they need to be read into different class type. How can I implement this?

class line_t {
public:
vector<point_t> P(2, point_t());

[Code].....

View 3 Replies View Related







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