Назад | Начало урока | Вперед
Содержание

Глава 3 стр 28

Считываем из префикса в переменные в десятичном виде

Итак, для преобразования двоичного числа, которое сохранилось у нас в массиве
в десятичное воспользуемся полученным нами опытом и напишем такой код:

int temp_l = 0;
int temp_h = 0;

temp_l = (int)buf[2];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[3];
if(temp_h<0)
temp_h = 256+temp_h;

temp_h = 256*temp_h;

size_file = temp_h+temp_l;

Теперь в size_file находится десятичное представление числа, которое содержится
в ячейке 2 массива buf.
Десятичное представление числа нам потребовалось для более привычного для нас
восприятия и манипулярования расчетов.

Ниже приведен код, в котором найдены все необходимые нам переменные
из префикса графич файла:

size_file :1954
chislo_str :25
size_card :1900
dlina_str :76

/////////////////////////
int temp_l = 0;
int temp_h = 0;
int size_file = 0; //размер исходного bmp-файла (2-й, 3-й байты)
int chislo_str = 0; //размер картинки в байтах (34-й, 35-й байты)
int size_card = 0; //количество строк в картинке (22-й, 23-й байты)
int dlina_str = 0; //длина строки в выходном файле

//--------------------------------
//Считаем данные из массива в переменные
temp_l = (int)buf[2];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[3];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
size_file = temp_h+temp_l;

temp_l = (int)buf[22];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[23];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
chislo_str = temp_h+temp_l;

temp_l = (int)buf[34];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[35];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
size_card = temp_h+temp_l;

dlina_str = size_card/chislo_str;

Полный текст программы будет таким:


// копируем посимвольно из файла в массив
//считываем из массива и преобразуем в десятичное значение
#include <stdio.h>
#include <fstream>
#include <iostream.h>
#include <string.h>

int main( int argc,char* argv[]) {

ifstream from1("bukva.bmp");//открываем файл для считывания
if(!from1) {

cout<<"Vhodnoi fail ne naiden:" ;
return (1);

}
ofstream to1("text2" ); // open for writing
if(!to1) {
cout<<"Vyhodnoi fail ne naiden:" ;
to1.close(); // always pays to be tidy
return (1);
}
//////////////////
const int len = 3000;
char buf[len];
char *p = buf;//сорентируем указатель на начало массива buf

char buf1[2];
char *p1 = buf1;//сорентируем указатель на начало массива buf1

char ch;
int n = 0;

while (from1.get(ch)) {

buf1[0] = ch;
strcpy(p,p1); //вставим
p++;
n++;
if(n>=len)break;
}

cout<<"n :" << n << endl ;

/////////////////////////
int temp_l = 0;
int temp_h = 0;
int size_file = 0; //размер исходного bmp-файла (2-й, 3-й байты)
int chislo_str = 0; //размер картинки в байтах (34-й, 35-й байты)
int size_card = 0; //количество строк в картинке (22-й, 23-й байты)
int dlina_str = 0; //длина строки в выходном файле

//--------------------------------
//Считаем данные из массива в переменные
temp_l = (int)buf[2];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[3];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
size_file = temp_h+temp_l;
cout<<"size_file :" << size_file << endl ;

temp_l = (int)buf[22];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[23];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
chislo_str = temp_h+temp_l;
cout<<"chislo_str :" << chislo_str << endl ;

temp_l = (int)buf[34];
if(temp_l<0)

temp_l = 256+temp_l;
temp_h = (int)buf[35];
if(temp_h<0)
temp_h = 256+temp_h;
temp_h = 256*temp_h;
size_card = temp_h+temp_l;
cout<<"size_card :" << size_card << endl ;

dlina_str = size_card/chislo_str;
cout<<"dlina_str :" << dlina_str << endl ;
//-------------------------------------------------

char ch1;
cout<<"Yype any char"<< endl ;
cin>>ch1;

////////////////////
from1.close(); // always pays to be tidy
to1.close(); // always pays to be tidy

return 0;

}

Результат:

size_file :1954
chislo_str :25
size_card :1900
dlina_str :76


Назад | Начало урока | Вверх | Вперед
Содержание

Hosted by uCoz