Scanning a file in binary mode and interpret the hex characters as actual numbers in C++ -


i know title might sound confusing. have simple question haven't been able solve yet.

lets imagine have file, opening hex editor shows has 2 characters inside, 0x1b , 0x00 (obviously unprintable). i'd take 1b00 in hex, 6912 in dec, opposed directly converting characters wrong , other questions saw asked. well, thats task want here. seems simple, i've tried wrong! though opening file in binary mode.

i have managed read characters individually, , mess around bit, never want, simple taking 2 hex characters, interpreting them hex number, , convert decimal.

sorry unclear idea, im not native speaker. appreciated, im sure you'll think quite noobish question :p

edit: sorry, apparently didn't explain myself properly. know might seem abstract, concrete little thing have struggled solved, yet haven't been able. maybe can ask way:

how can scan character in binary mode, lets 0x1b, , convert actual 1b characters. that.

sounds want read file raw data, , display on screen in decimal? super easy!

int main() {     std::ifstream myfile("filename.data", std::ofstream::binary);     uint16_t number;     char* buffer = (char*)(&number);     while(myfile.read(buffer, sizeof(number))) {         std::cout << number << ' ';     } } 

the reason it's easy there's no hexidecimal involved. file saved series of bytes, each byte holds 1 of 256 values. aren't hex, they're series of values. if read 2 bytes uint16_t, easiest way interpret 2 bytes single unsigned 2 byte value. , streaming out uint16_t will, default, display value in decimal. there's no hexidecimal involved. hexidecimal saw in hex editor because hex editor interprets bytes hex values.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -