Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting bytes to "long" types

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
Hello there...

Maybe I am missing or forgetting something, but is there a good, clean way to convert an array of bytes to a long or a short integer or any other multi-byte type?

My trouble is that I am trying to convert an array created like:
Code:
char* array = malloc(10000);

to an array of short integers
and I want to do this without iterating through the whole bte array.
Is this possible somehow?

Thanks.

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 

int fac = sizeof(long) / sizeof(char);
long* la = (long *)realloc(array, sizeof(array)* fac);

'array' data is still inside 'la'.But the data is aligned to char elements.
So each la contains 4 chars from 'array' in each of its 4 bytes elements (sizeof long).
So if you like to save it you'll need a function that itterates through the array(s), to reassign 'array' to 'la', or to align the data to long that is already in 'la'.
 
hi

do it this way

char * arr=(char*) malloc(1000);
short *s_arr=(short*)arr;

do any manipulation you wish with s_arr or arr

only thing is s_arr would be treated as short*
while arr would be treated as char *

hawapani
 
You may run into an endian problem if you are doing this on a pc.

the number 1 on a big endian machine in a short will be represented as

0000000000000001

Seems logical... 2 bytes, first byte all zeros, second byte, last bit set representing 1. But on a Little Endian machine (such as windows) it will be represented as

0000000100000000

It is a pain in the butt!!!

If however you take the s_arr pointer and assign shorts to each element you will not see this problem. If you are assigning characters and converting them to shorts, realize that you may need to do some byte swapping.

Matt
 
Matt, you are absolutely right on the big-little eggdian problem. This is a problem that complicates most of the OS independent software out there...

But, hawapani did not answer my question with that casting... And the answer I guess it is...It is not possible to do it automatically.

It was stupid of me to think it would be possible anyway. One can write functions that would convert an array of bytes to an array of shorts (integers, not pants:)) by manually assigning each value of the byte to the low/high order byte in a newly created short int array.

What
Code:
char* c_arr;
short int * sh_arr = (short int*)c_arr;
would mean when
c_arr = "ab"?

we find that sh_arr will have just 1 element and
*sr_arr = 0x6566.

So, this is NOT what I would expect... (i.e. 0x0065,0x0066) Actually, is the whole problem of wide and 8 bit characters; conversions back and forth, for which there are some functions in the ANSI C...

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
I agree that the Nosferatu that casting the way
i did may not solve your problem.

But friend casting the way i did is a very strong programming technique when it comes to writing memory management programme. This technique may be used efficient for writing file compression programme also.

i am sorry for not mentioning the endian problem, Yeah
here one has to cast one type to another all the time.

hawapani
 
Hawapani...

I also agree with you that that casting type is a strong programming technique. I use it extensively both in my C and C++ programs and it sits at the very base of C++, with all that inheritance and virtual stuff.

But again, that was not what I wanted. Anyway, it is a good thing you mentioned it. I don't know if there's a FAQ on this - casting - , if not, it worths if someone would spend some time on it. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top