Can't convert bin. ascii to float

Started by
15 comments, last by TDP owns 17 years, 7 months ago
But why, it woks fine, aint the value by default 0?
Advertisement
Quote:Original post by TDP owns
Initial value, zero i guess.
Im using the k as a offset for reading through the ascii string and
increasing it by one each x-loop


There is no default initial value in C/C++. Unless you initialize the variable manually, you don't know the value of k. To make my statement simple, k is probably not 0 at the beginning of the loop.

There are also numerous other errors in the code, as well as some style problems.

1) don't use litteral constants - use symbolic constant instead:
int HM[768]|768]; // badchar Buffer [589823]; // bad--const int HM_SIZE = 768; // far betterint HM[HM_SIZE][HM_SIZE];

Your buffer size is wrong. You want to read 768x768 pixels. Thus, you have to read 768*768=589824 bytes, not 589823. I guess that you there is some kind of misunderstanding in the way you see arrays.
When you declare an array, you declare its whole size. When you use an array, don't forget that the index of its first element is 0. For example, if you have to create an array that will contain 4 integers, you declare it as:
int array[4];

But if you want to use it, you can only access to indices 0 to 3 (0,1,2,3 => 4 indices).
Same problem: when converting your bytes to floats, the loop maximum indices are wrong. To loop over 768 pixels, you must loop from 0 to 767 included. Or, if you don't want to include the last value (common idiom), then the stop condition is "<768" (or better "<HM_SIZE").

Your code shoudl read:
const int HM_SIZE = 768;float HM[HM_SIZE][HM_SIZE];void LoadFile(){  // char Buffer [589823]; // NEVER DO THAT. It allocates an array ON THE STACK  // which is supposedly a limited resource.  std::vector<char> buffer(HM_SIZE*MH_SIZE); // allocates a buffer  long k = 0;  ifstream File("hm.b",ios::in | ios::binary);  File.read (&buffer.front(), buffer.size());  File.close();  for (int y = 0; y< HM_SIZE; y++ )  {    for (int x = 0; x < HM_SIZE;x++)    {      float z = (float)buffer[k];      // there is a simpler way to do this, which does not need you to      // maintain a 'k' variable: float z = (float)buffer[y*HM_SIZE+x];      HM [x] [y] = z;      k++;    }  }}


To answer your very last question: yes, there is other ways to inspect variable values. The best one is probably the debugger which is integrated to your IDE.

HTH,
Thx, your post was very helpful.
It kinda seems logical now...
C++/VB differences are starting to confuse me...
I've got to do lots of learning about this stuff, maybe should have done a hello world first or smth...
Quote:Original post by TDP owns
Thx, your post was very helpful.
It kinda seems logical now...
C++/VB differences are starting to confuse me...
I've got to do lots of learning about this stuff, maybe should have done a hello world first or smth...


Then I suggest you to have a look to the running C++ Workshop (see sig for link). The only thing you have to do is to buy a cheap book (you can probably find a used one if you don't want to pay the full price), follow the chapters and ask if there is something you don't understand. We'll be glad to help.
thx, i'll try to get one.

Edit:
I've just finished modding my code and now it works fine.
All heights are accurate.
Thank you!
Quote:Original post by TDP owns
thx, i'll try to get one.


You won't regret it. First, read the workshop introduction, as it need you to buy a specific book.
"Sams Teach Yourself C++ in 21 Days (5th Edition)" @ amazon.com this one?
idk if they ship here, if i can i'll get one

This topic is closed to new replies.

Advertisement