Converting a little endian number into long
Code:
long x_size = buff2[3] << 24 | buff2[2] << 4 | buff2[1] >> 4 | buff2[0] >> 24;
The original value: 6630836
The value stored in file: b4 2d 65 00
I read the number as
Code:
char *buff2[5] = { 0 };
f.read(buff2, sizeof(char)*4);
The result is ffffffff (-1)
Are there any compact ways I can convert the number into the right form?
I guess I am not doing it right for the moment....
Thanks
Jack
Re: Converting a little endian number into long
The size of the char is 8 bits. So how do you shift it 24 bits left (or right)? :confused:
/Edit: besides: why not just read this long value into the long variable?
Re: Converting a little endian number into long
I thought I was doing this
00000000 1100101 00101101 10110100
-----------
|________________________>24bits???
Incorrect I think, what is your insight?
Going to tell my story though,
I have several x files to pack into one pck file to avoid multi-opening.
So I read the file size by ftell then write it to the pck file.
I must confess there is some inconsistency. I used fwrite (const void *)&filesize, sizeof(long),1, f_ptr);
then the data. Then reading it back into memory using the above format.
Any suggestions that I can do it better?
Thanks
Jack
Re: Converting a little endian number into long
Also, you are reading into an array of 5 char pointers, not 5 chars. I think you meant char buff2[5].
The algorithm you are trying to use is also wrong. Try to get that right to start with, i.e. work out by how much you need to multiply each byte to get what you're looking for, then think about how to code that.
Re: Converting a little endian number into long
Yes, you think right! You did it incorrect. See my previous post.
Re: Converting a little endian number into long
Sorry Alan, Yes, i meant char buff2[5];
Victor, let me take a thought
Code:
x_size = (long) buff2;
The value seems to be off though...
Jack
Re: Converting a little endian number into long
Quote:
Originally Posted by
lucky6969b
Sorry Alan, Yes, i meant char buff2[5];
So what is your actual code? :confused:
This one:
Quote:
Originally Posted by
lucky6969b
Code:
char *buff2[5] = { 0 };
f.read(buff2, sizeof(char)*4);
The result is ffffffff (-1)
Or this one:
Code:
char buff2[5] = { 0 };
f.read(buff2, sizeof(char)*4);
Re: Converting a little endian number into long
Quote:
Originally Posted by
lucky6969b
Sorry Alan, Yes, i meant char buff2[5];
Victor, let me take a thought
Code:
x_size = (long) buff2;
The value seems to be off though...
Jack
No it is wrong! :thumbd:
Why not just _open a file and then _read to the long buffer exactly 4 bytes (or the sizeof(long))?
Re: Converting a little endian number into long
This one:
Code:
char buff2[5] = { 0 };
f.read(buff2, sizeof(char)*4);
Thanks a lot
Update
Error 2 error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'long' to 'char *' l:\shader approach\partiii\chapter 14\xfiledemo\d3dutil.cpp 207 XFileDemo
Code:
long buff2;
f.read (buff2, sizeof(long));
Sorry, still couldn't make it
I am using this method because I need Unicode support.
Update:
I also tried this with no luck
Code:
x_size = (*buff2 >> 24) | ((*buff2 & 0x00ff0000) >> 8) | ((*buff2 & 0x0000ff00) << 8) | (*buff2 << 24);
Thanks
Re: Converting a little endian number into long
I found a workaround this.. Thanks a lot
Code:
f.read(reinterpret_cast<char *>(&buff2), sizeof(long));