I got int with a signed number, and i want to split it to array of bytes, in little endian.
any ideas?
Printable View
I got int with a signed number, and i want to split it to array of bytes, in little endian.
any ideas?
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
great. thanks :)
how you know all that stuff?
glad that you are happy. thanx
but I am not a guru ;)
cool.
thanx a lot.
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..
oh ! really
LOL
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
martin, as you are always informative,
this is a good piece of information.
thanx again,
Paresh
I dont think underwater would get full marks for his assignment if he used an api function to answer the question...Quote:
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
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
grrr :)
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
this going crazy.. come on guys i am not gonna college LISP and theoritical programs...
:D ;) :D
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