Click to See Complete Forum and Search --> : Hi, hard problem for you pareshgh


underwar
February 27th, 2003, 08:41 PM
I got int with a signed number, and i want to split it to array of bytes, in little endian.

any ideas?

pareshgh
February 27th, 2003, 09:23 PM
I hope this will help you ( I wrote a console program :D and tested which will give you the result required,

-------------
// byte representation of a long number
int a = 123456789; //111 01011011 11001101 00010101

byte []b = new byte[4];

b[0] = (byte)(a & 0xFF);
b[1] = (byte)((a & 0xFF00) >>8);
b[2] = (byte)((a & 0xFF0000) >> 16);
b[3] = (byte) ((a & 0xFF000000) >>24);

Console.WriteLine(b[0]);
Console.WriteLine(b[1]);
Console.WriteLine(b[2]);
Console.WriteLine(b[3]);
----------------------

b[0] is and'ing with 255 so get the first byte

b[1] is and'ing only 2'nd byte and rest are zero'ed and then right shifted 8 bytes so as to get byte what's in it.
and so on.

Paresh

underwar
February 27th, 2003, 10:00 PM
great. thanks :)
how you know all that stuff?

pareshgh
February 27th, 2003, 10:07 PM
glad that you are happy. thanx
but I am not a guru ;)

savagerx
February 28th, 2003, 08:15 AM
cool.

pareshgh
February 28th, 2003, 02:29 PM
thanx a lot.

HH2000
March 2nd, 2003, 08:45 PM
bahahhahaha..
You know what this thread sounds like..

This thread sounds like a high school kid looking for someone to do his homeowork..
:D

**** some ppl can be so tricksy(LOTR)....


I guess it beats renting a coder at rend a coder web site..

pareshgh
March 2nd, 2003, 09:53 PM
oh ! really
LOL

MartinL
March 3rd, 2003, 04:42 AM
Hay you guys! Have you ever heart about the windows socket function like: ntohl()? It takes big indian long and conerts it into the little-indian on Intel processors... :)

You can import them into the C# and use them...

I know, it is just alternative and the paresh's solution works too...

Martin

pareshgh
March 3rd, 2003, 10:43 AM
martin, as you are always informative,
this is a good piece of information.

thanx again,
Paresh

HH2000
March 3rd, 2003, 02:07 PM
Originally posted by MartinL
Hay you guys! Have you ever heart about the windows socket function like: ntohl()? It takes big indian long and conerts it into the little-indian on Intel processors... :)

You can import them into the C# and use them...

I know, it is just alternative and the paresh's solution works too...

Martin

I dont think underwater would get full marks for his assignment if he used an api function to answer the question...

pareshgh
March 3rd, 2003, 02:12 PM
hahaha
lol
he isn't underwater
he is underwar. (with me :D )

and yes, u r right. he won't get the full marks for his assignement. if i would be his professor i wdn't gave the internet access and then instructed to try on his own
;) :D

LOL again

take it easy,
Paresh

underwar
March 3rd, 2003, 05:55 PM
grrr :)

MartinL
March 4th, 2003, 01:25 AM
Hm... If this is the point (to make things more difficult), I would recommed him to write the routine in LISP and use it in C#...

Or even better, to use monolit microcomputer, write it in assembler and comunicatate with the monolit mc using COM port... :D:D:D

Martin

pareshgh
March 4th, 2003, 01:42 AM
this going crazy.. come on guys i am not gonna college LISP and theoritical programs...
:D ;) :D

Brian Lasota
July 28th, 2003, 06:27 PM
Ok how would you go in the oposite direction.

I have a file wrriten in binary from an old legacy program so there is no serialization attributes. The first two bytes is an integer that allows me to determine the layout of the rest of the file.

I read in hte first two bytes into an array and want to put that into an integer. In addition there will be doubles, longs, int and string values wrriten into the file. Do I do the general same thing for all numeric values (doubles, int, longs).

Thanks