|
-
May 6th, 2011, 03:41 AM
#1
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
Last edited by lucky6969b; May 6th, 2011 at 03:45 AM.
-
May 6th, 2011, 03:46 AM
#2
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)? 
/Edit: besides: why not just read this long value into the long variable?
Last edited by VictorN; May 6th, 2011 at 03:48 AM.
Reason: "besides" was added
Victor Nijegorodov
-
May 6th, 2011, 03:51 AM
#3
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
Last edited by lucky6969b; May 6th, 2011 at 03:54 AM.
-
May 6th, 2011, 03:55 AM
#4
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.
-
May 6th, 2011, 03:56 AM
#5
Re: Converting a little endian number into long
Yes, you think right! You did it incorrect. See my previous post.
Victor Nijegorodov
-
May 6th, 2011, 04:00 AM
#6
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
Last edited by lucky6969b; May 6th, 2011 at 04:04 AM.
-
May 6th, 2011, 04:06 AM
#7
Re: Converting a little endian number into long
 Originally Posted by lucky6969b
Sorry Alan, Yes, i meant char buff2[5];
So what is your actual code? 
This one:
 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);
Victor Nijegorodov
-
May 6th, 2011, 04:11 AM
#8
Re: Converting a little endian number into long
 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! 
Why not just _open a file and then _read to the long buffer exactly 4 bytes (or the sizeof(long))?
Victor Nijegorodov
-
May 6th, 2011, 04:12 AM
#9
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
Last edited by lucky6969b; May 6th, 2011 at 04:25 AM.
-
May 6th, 2011, 05:19 AM
#10
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));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|