J
J. Campbell
I'm a novice with c/c++ and have been reading Eckel's book. I'd like
some feedback on using this method. What I need to do is treat a
string as numeric data. I know how to write functions to convert
between data types, but was thinking that this isn't really necessary
since I can access the memory directly in c. I want to treat the text
as native integer so that I can perform fast bit-level manipulations.
This code works fine...but I'm wondering...is it portable? My main
concern is the byte order with the near byte being the leas
significant. I want to be *sure* that if I encode my text data as int
on one machine, that I can get my original text back on another. Will
this method work cross-platform? What are the dangers of doing this?
Is there an easier way? This is just sample code to demonstrate what
I am talking about...it's not meant to be useful. I appreciate any
input you can offer.
Joe
[code follows]
#include <string>
#include <iostream>
using namespace std;
int main(){
cout << "Enter some text" << endl;
string text;
getline(cin, text);
int tlen = text.length();
int extrabytes = tlen % sizeof(int);
cout << "Text has " << tlen << " characters. Must pad with "
<< sizeof(int) - extrabytes << " extra bytes for even "
<< "multiple of (int)" << endl;
tlen += sizeof(int) - extrabytes;
char chararray[tlen];
cout << endl << "CHAR ARRAY (index/value)" << endl;
for(int i = 0; i < tlen; i++){
if(i < text.length())
chararray = text;
else chararray = (char)0;
cout << i << " " << chararray << endl;
}
cout << "String transfered to char array...and padded to fit"
<< " an int array\n"
<< "char array location " << &chararray << endl;
void* pa = chararray;
unsigned int* pa2 = ((unsigned int*)pa);
cout << "char array location will be dereferenced as"
<< " unsigned int..." << endl;
int arraylenint = tlen / sizeof(int);
unsigned int intarray[arraylenint];
cout << endl << "UNSIGNED INT ARRAY (index/value)" << endl;
for(int i = 0; i < arraylenint; i++){
intarray = *pa2++;
cout << i << " " << intarray << endl;
}
cout << "\n\nNote that each unsigned int (4-bytes on my machine)\n"
<< "takes the near byte as the least significant byte...\n"
<< "for example, if \"joec\" is entered, the output will be\n"
<< "1667592042...or \n"
<< "106*(256^0) + 111*(256^1) + 101*(256^2) + 99*(256^3)\n"
<< "where the ASCII values for j=106, o=111, e=101, c=99\n";
return 0;
}
[end code]
some feedback on using this method. What I need to do is treat a
string as numeric data. I know how to write functions to convert
between data types, but was thinking that this isn't really necessary
since I can access the memory directly in c. I want to treat the text
as native integer so that I can perform fast bit-level manipulations.
This code works fine...but I'm wondering...is it portable? My main
concern is the byte order with the near byte being the leas
significant. I want to be *sure* that if I encode my text data as int
on one machine, that I can get my original text back on another. Will
this method work cross-platform? What are the dangers of doing this?
Is there an easier way? This is just sample code to demonstrate what
I am talking about...it's not meant to be useful. I appreciate any
input you can offer.
Joe
[code follows]
#include <string>
#include <iostream>
using namespace std;
int main(){
cout << "Enter some text" << endl;
string text;
getline(cin, text);
int tlen = text.length();
int extrabytes = tlen % sizeof(int);
cout << "Text has " << tlen << " characters. Must pad with "
<< sizeof(int) - extrabytes << " extra bytes for even "
<< "multiple of (int)" << endl;
tlen += sizeof(int) - extrabytes;
char chararray[tlen];
cout << endl << "CHAR ARRAY (index/value)" << endl;
for(int i = 0; i < tlen; i++){
if(i < text.length())
chararray = text;
else chararray = (char)0;
cout << i << " " << chararray << endl;
}
cout << "String transfered to char array...and padded to fit"
<< " an int array\n"
<< "char array location " << &chararray << endl;
void* pa = chararray;
unsigned int* pa2 = ((unsigned int*)pa);
cout << "char array location will be dereferenced as"
<< " unsigned int..." << endl;
int arraylenint = tlen / sizeof(int);
unsigned int intarray[arraylenint];
cout << endl << "UNSIGNED INT ARRAY (index/value)" << endl;
for(int i = 0; i < arraylenint; i++){
intarray = *pa2++;
cout << i << " " << intarray << endl;
}
cout << "\n\nNote that each unsigned int (4-bytes on my machine)\n"
<< "takes the near byte as the least significant byte...\n"
<< "for example, if \"joec\" is entered, the output will be\n"
<< "1667592042...or \n"
<< "106*(256^0) + 111*(256^1) + 101*(256^2) + 99*(256^3)\n"
<< "where the ASCII values for j=106, o=111, e=101, c=99\n";
return 0;
}
[end code]