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


ADVERTISEMENT

C++ :: Not Able To Read Data From Text File And Convert It To Integer

Mar 25, 2013

I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25

30 40 40 30 40

I am using getline, and then converting it to int by using atoi. But my code is not working.

Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] ....

View 10 Replies View Related

C++ :: Convert String Which Represents Date To Integer?

Nov 13, 2014

I would like to know how I can convert a string which represents a date to an integer.

#include <iostream>
using namespace std;
#include <string>
int main(){
string actual_date("18/10/2000");
int date = ????????

View 2 Replies View Related

C++ :: Encrypt Char Array With Binary Data

Apr 14, 2013

I am trying to encrypt a char Array with binary data.

I think I understand the basic of Encryption / Decryption but I fail to see how to implement something.

I am trying to have a "key" that needs entered so the data can become readable executable. The program I am encrypting is a small console window with a message with the text "A secret message from your friend" (Not that it matters).

I have the binary data witch I can copy and what not. But how go about Encryption and then decrypt it and not destroying the data.

View 6 Replies View Related

C++ :: Binary IO - Read 4 Byte Int From A File

Feb 26, 2012

Trying to write very simple code to read a 4 byte int from a file.

code:

int tester;
FILE *fp;
fp=fopen("/home/bdluser/skeleton.blx","rb");
fread(&tester,sizeof(int),1,fp);
printf("tested 1 byte read should be 1: %i",tester);

I have tried editing the binary file....it outputs similar large numbers

00000000000000000000000000000001 outputs 808464432
0x00000001 outputs 808482864
0x10000000 outputs 808548400
10000000000000000000000000000000 outputs 808464433
x01x00x00x00 outputs 813183068
x00x00x00x01 outputs 813183068 same....

etc.etc. It just keeps returning the same large integers. I thought they were memory addresses but its consistent output/input so I guess not.

View 2 Replies View Related

C Sharp :: Convert Multiline Text Box Value Into Byte

Aug 31, 2012

protected void btnUpload_Click(object sender, EventArgs e){
 string pic = ASCIIEncoding.ASCII.GetString(fileUpload1.FileBytes);
 TextBox1.Text = value.ToString();
//Here i get pic[40385]

[Code] ....

On two different event i get two different value which cause file not open due to file corrupted

View 1 Replies View Related

C/C++ :: How To Convert Bytes Of Data To String

Jan 24, 2014

How to convert bytes of data to string using c programming , I searched but i didnt get appropriate one .

Example : 112131(bytes) to string ("some sample")

View 4 Replies View Related

C :: Program To Read Binary Data And Print It - File I/O Error

Mar 14, 2013

Objective of this program is to read binary data from a file and print it.

Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;

[Code] .....

But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!

I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.

View 4 Replies View Related

C++ :: Writing Text Data To A Binary File With Fstream

Jul 9, 2014

fstream ifs;
ifs.open ("data.str", ios::binary | ios:ut);
char *data1 = "data1";
char *data2 = "data2";
ifs.write(data1, strlen(data1));
ifs.write(data2, strlen(data2));

when i this,data2 is not going under data1, i thought each write starts on a new line?

View 4 Replies View Related

C++ :: How To Convert String To Double Without Changing Actual Data

Jun 3, 2013

I have to convert string to double. i'm using "atof" function to achieve same.

I have string as "0.0409434228722337" and i'm converting with "atof" But i'm getting double value as "0.040943422872233702". Why it adds 02 additionally at the end?

More example :

"0.0409434228722337" converts to "0.040943422872233702"
"0.067187778121134" converts to "0.067187778121133995"

Is there any other possibility to convert string to double without changing data ?

View 5 Replies View Related

C :: Unable To Read Data From Text File And Print It

May 12, 2014

I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.

my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)

For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)

*This is the code for my read file function*

Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.

Code:

polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];

[Code]....

This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.

Code:

polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);

[Code]....

This is the rest of the code if you need to refer to other codings.

Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/

[Code]...

View 6 Replies View Related

C++ :: Sort Data From Text File Into Array As Its Being Read?

Feb 12, 2013

I am suppose to make a program that reads in data from a text file (integers only) and sorts them as it inserts them into an array of size 10. I did this using an insertion sort, which worked great. But now I am being told that I need the function has to read ALL of the numbers in the text file, not just the first 10, and I am not allowed to store them THEN sort, it has to be sorted as being stored.

This is what I have for sorting first 10

void sortArray(int iArray[])
{
string fileName = "";
fstream inFile;
int tmp = 0;

[Code]....

View 3 Replies View Related

C++ :: Generate A Binary String Based On Hex Data

Apr 23, 2013

Generate a binary ascii characters(weird character ) based on Hex:54313032202020303030 data in C++

View 3 Replies View Related

C++ :: Convert String Variable For The Year To Integer Data Type

May 12, 2014

My intent was to convert the string variable for the year to an integer data type. The code compiles but now cannot run on my system. I'm not sure what's going as to what the program is displaying.

Objective: Prompt the user for two years. Print all Comedy movies that were released between those two years.

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
struct Movie {
string name;

[Code] .....

View 2 Replies View Related

C++ :: Open A Text File Then Read Data And Print It Back?

Nov 5, 2013

I am in an "intro" C++ course and am having trouble with a section of my current project.

We are required to open a text file, read the data from that file, and print it back.

However, we are supposed to let the user input the name of the file that is to be opened. This is the code I have so far. It is not opening anything.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 30;
typedef char CharArray[NUM_QUESTIONS];
string fileName;

[Code]...

View 2 Replies View Related

C :: Store Data In Binary File Then Use It To Find Inverse Of Matrix Form Of Data

Dec 6, 2013

I need to find inverse of a matrix data in binary file should be used to form matrix.its not finding inverse correctly rest is working good.

Code:
#include <stdio.h>
void createbin();
void display();
void inverse();
int main()
{
createbin();
display();
inverse();

[Code] ....

View 4 Replies View Related

C++ :: Combine Multiple Vector Containing Binary Data To String?

Mar 16, 2012

Two questions:

1. I have some vector<unsigned char> containing binary data. I would like to combine them into one std::string. How is the correct way to accomplish this?

This is my best guess for sample code:

Code:
vector<unsigned char> data; //conatins some data
vector<unsigned char> data2; //contains more data
string temp(data.begin(), data.end());
temp.append(data2.begin(), data2.end());

Will this code work with binary data, or will it null terminate?

2. A similar problem.. I have some unsigned char* variables, and I want to combine them into one std::string. How can I accomplish this? will the member append() work here? or will it null terminate? Something like:

Code:
unsigned char* data; //conatins some data
unsigned char* data2; //contains more data
string temp(reinterpret_cast<const char*>(data));
temp.append(string(reinterpret_cast<const char*>(data2)));

Will the above sample code work without null termination?

View 4 Replies View Related

C :: Program That Can Implement Functions That Store / Get And Deletes Text / Binary Data To Given Memory Area

Sep 19, 2013

I got an assignment at school asking for a program that can implement functions that store, get and deletes text/binary data to a given memory area. We are supposed to make kind of like a tiny-mini OS..any links to some tutorials or explanations hon ow to understand this and be able to make a program like this on my own?

View 9 Replies View Related

Visual C++ :: Employee Payroll - Read Data From Text File And Sort

Nov 11, 2013

i tring to write a employee payroll code with functions and read data from the txt. file and also bubble sort that sorts and displays starting from employees last name and calculations.

//txt file//
Hours Pay rate empID first name last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper

and here is my code

Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
void tellUser();

[Code] .....

here are the errors the are giving

Code:
payroll.cpp: In function "int bubbleSort()":
payroll.cpp:52:8: error: "lastpos" was not declared in this scope
payroll.cpp:52:18: error: "numemp" was not declared in this scope
payroll.cpp:56:10: error: "swapmade" was not declared in this scope
payroll.cpp:57:16: error: "i" was not declared in this scope

[Code] ....

View 14 Replies View Related

C# :: Store Each Char Of A String Into Byte-array?

Apr 25, 2014

I am trying to store each char of a string(string a ="1100") into a byteArray ( byte[] byteArray = new byte[4]. its not showing any error but its storing like below:

byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48

and what i want is

byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0

I don't know why but its replacing 1 with 49 and 0 with 48.what am I doing wrong or how to do this?

my code is as below

byte[] byteArray = new byte[4];)/>
int binArrayAdd = 0;
string a ="1100";
foreach (char Character in a)
{
byteArray [binArrayAdd] = Convert.ToByte(Character);
binArrayAdd++;
}

View 4 Replies View Related

C++ :: Read Phone Number Into Array And Can't Use String Data Type

Jan 13, 2013

I have an assignment where I need to read in a phone number into any array and I can't use the string data type. Assuming I need to use the getline function, how do I do this? I'm guessing it's with the cstring or .c_str() functions? I don't know.

View 4 Replies View Related

C++ :: Add Data To Text Files Which Are Required To Store 3D Scan Data

Jul 10, 2013

I have written the following code to add data to text files which are required to store 3D scan data (I have to calculate the y-coordinate). My code seems to work except that it stops working when I want to create more than ten text files i.e. the directory I am trying to store them in will not hold any more than ten text files. Code is shown below.

#include <string>
#include <locale>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

[Code] ....

View 1 Replies View Related

C++ :: Data Input Into A Text File While Not Deleting Original Data

Apr 19, 2013

I want to input data into text file while not deleting the original data in the file and I use something as

ofstream writefile;
writefile.open("example1.txt");
if (writefile.is_open()) {
for(j=0; j<N; j++) {

[Code] ....

But this will delete the original data.

View 3 Replies View Related

C++ :: Encoding Byte Array Data?

Apr 23, 2012

I'm trying to parse some binary data in the form of an array of bytes and I've come across something that is confusing me related to the representation of data as chars versus ints. It's a bit of a long story, but the byte array contains a mixture of character data and integer data which I' having trouble unravelling. The problem seems to arise from the issue below:

Code:
const char * ch_arr = "abcd";
const unsigned int * ui_arr = (const unsigned int*)ch_arr;
cout << ui_arr[0] << endl;
unsigned int ui = 'a';
ui = ui << 8;
ui |= 'b';
ui = ui << 8;
ui |= 'c';
ui = ui << 8;
ui |= 'd';
cout << ui << endl;

I expected both the output lines to be the same, since they contain the same bytes (I believe), but I get:

Code:
1684234849
1633837924

View 4 Replies View Related

C++ :: String Stream Object And Text File Data Access

Jan 21, 2014

I have two puzzling issues I am dealing with.

Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.

Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?

View 7 Replies View Related

C++ :: Loop To Read In Data And Check Data

Oct 8, 2013

struct receivers
{string fname, lname, team,;
int receptions, yards, TDs, longest,rec20, fumbles, yac, firstdown;
double, averagepergain, averageperrec
}

View 2 Replies View Related







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