![]() |
|
#1
|
|||
|
|||
Convert from FILE .read()char array to integer arrayHello,
I have data retrieved from .read() file i/o operations which works on char arrays. I'm looking at cleaner ways to convert into arrays of 16 bit and 32 bit unsinged integers. One way to do it is : CPP / C++ / C Code:
Another question is that if I encapsulate the .read into a function that outputs with C++ strings, is there an easy way to do the same thing, i.e. convert from the char array to shor int arrays? |
|
#2
|
|||
|
|||
Re: Convert from FILE .read()char array to integer arrayQuote:
Quote:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Convert from FILE .read()char array to integer arrayQuote:
See Footnote. I assume that your file contains binary data. If not, then there's not much point in using the istream read() function, right? I mean, if you want to read integer values from a text file, just use ">>" to read the formatted data. (Still using vectors to hold the values, not arrays.) Anyhow for binary files: CPP / C++ / C Code:
I have attached a zipped binary file for testing. The file contains hex bytes Code:
Here's the output: Code:
As you can see, on my system shorts are two bytes long and internal storage is little-endian. Quote:
(If I had a numeric variable and I wanted to convert its value to a C++ string, I would probably use a stringstream object, but I'm not sure that's what you have in mind.) Regards, Dave Footnote: If you really, really need to use arrays rather than C++ vectors (and there are times when you may have to do this), then the way to keep from writing past the end of the arrays is to create code that doesn't write past the end of the arrays. I know this sounds silly and maybe even a little sarcastic, but I mean no disrespect. It is the programmer's duty to make sure the code he/she writes can never, ever write past the end of the arrays. Last edited by davekw7x : 20-Jul-2008 at 14:02.
|
|
#4
|
|||
|
|||
Re: Convert from FILE .read()char array to integer arraydavekw7x,
Thanks very much for the vector advice. Quote:
Quote:
GOAL A) is to not use arrays at all nowhere in my code. Even with my integer class, I had to do the intermediate pass with arrays sometimes. GOAL B) is to get rid of the amateurish way to take 4 chars (usually as subsets of C++ strings), do the sign checking and create an unsigned 32 bit integer. (also the same with the 16 bit short ints) You see I have packets where in a function I process the header and footer, leaving the middle payload for another function to process as a C++ string. This payload has the 32 and 16 bit arrays I was talking about and am exploring neater ways to do this. Not all my packets have the same size of 16 bit arrays. Maybe I could use a few structs for the couple packet sizes. Off the top of your head, do you see another solution for goal B??? Thanks a lot! Frank |
|
#5
|
||||
|
||||
Re: Convert from FILE .read()char array to integer arrayQuote:
Quote:
If you know that the bytes are in correct order, (file-endianness is the same as machine-endianness), then you are through, as I showed in the printout. If the bytes must be reversed, then reverse them. So: What's the question? (But I asked that already.) There's no "sign checking or other "creation." They simple are in memory assigned to a particular variable. Quote:
Read the bytes from the file into wherever the heck you want them to go. If they are binary numerical values and the bytes in the are in the correct order for your machine, then read them into a variable of the correct type (length). If your program has to read bytes from a file or a network packet (you always have to know whether the ints were written in big-endian order or little-endian order) you can create code that reads a byte at a time into a numeric variable of correct size (using logical shift and "OR" operations) and store in whatever endianness is natural for your machint. In other words the same source code will work whether it is compiled on a big-endian machine or a little-endian machine. Network programmers use functions like htonl(), htons() to do it in a way that is more-or-less invisible to programmers. Compiler configuration by the vendor has already taken into account whether the bytes going between machine and network need to be reversed. These and many other functions used in sockets programming are not standard C or C++ library functions, but are (almost) always supplied with serious compilers. Quote:
Regards, Dave Footnote: The subject of endianness must always be taken into account whenever you are dealing with binary multi-byte numeric data items. If you are making your own network protocol, use whatever you want, but you should be aware that all significant network standards transfer bytes in big-endian fashion. That means for people using Intel/AMD PCs (and all of the other little-endian systems out there) always have to reverse byte order when transmitting or receiving multi-byte binary numeric data. |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Power Calibration Error In Nero Fix (hopefully) | matt3678 | Computer Hardware Forum | 55 | 17-Sep-2008 19:54 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 20:03 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 21:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The