473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

byte to LONG


I received byte by byte
0x8E byte1
0x3D byte2

0x64 byte3
0x5F byte4

How can I construct to be LONG 0x645F8E3D (shift left >> 16)

Thanks.
Nov 14 '05 #1
14 2937
Magix wrote:
I received byte by byte
0x8E byte1
0x3D byte2

0x64 byte3
0x5F byte4

How can I construct to be LONG 0x645F8E3D (shift left >> 16)


Determine where each by should go and then shift it there and then
bitwise or it with a long.

long result = 0;
unsigned char byte2 = 0x3D;

result |= byte2 << 8;

You can figure out the rest from there I guess.

(I have made the assumption that an unsigned char is 8 bits in the above
and that long is at least twice as large an unsigned char. Not
unreasonable, but not guaranteed. To get around the bytes are not 8 bits
problem you can use CHAR_BIT instead of 8, result |= byte2 << CHAR_BIT;)

--
Thomas.
Nov 14 '05 #2
Thomas Stegen wrote:

Magix wrote:
I received byte by byte
0x8E byte1
0x3D byte2

0x64 byte3
0x5F byte4

How can I construct to be LONG 0x645F8E3D (shift left >> 16)


Determine where each by should go and then shift it there and then
bitwise or it with a long.

long result = 0;
unsigned char byte2 = 0x3D;

result |= byte2 << 8;

You can figure out the rest from there I guess.

(I have made the assumption that an unsigned char is 8 bits
in the above
and that long is at least twice as large an unsigned char.


I don't think that you made that assumption.
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:

(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)

CHAR_BIT isn't part of it.

--
pete
Nov 14 '05 #3
On Sat, 09 Oct 2004 02:22:25 +0000, pete wrote:
A portable expression to make a long unsigned value of

What about where sizeof(long) < 4 ? Or endianess ?

Nov 14 '05 #4
>On Sat, 09 Oct 2004 02:22:25 +0000, pete wrote:
A portable expression to make a long unsigned value of ...

In article <news:pa****************************@Utel.no>
Nils O. Selåsdal <NO*@Utel.no> wrote:What about where sizeof(long) < 4 ?
Not a problem, because even if sizeof(long)==1, unsigned long
must still be at least 32 bits long, and hence hold values up
to 0xffffffffUL inclusive.
Or endianess ?


Not a problem either -- the expression depends only on the values
of the inputs, not their representations.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
pete <pf*****@mindspring.com> writes:
[...]
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:

(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)

CHAR_BIT isn't part of it.


Assuming that the input bytes are 8-bit bytes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
[...]
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:

(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)

CHAR_BIT isn't part of it.


Assuming that the input bytes are 8-bit bytes.


Absolutely not.
There is no value of CHAR_BIT which could cause the value
of above said expression, to equal anything other than 0x645F8E3DLU
when the values below are used in the expression.
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4

--
pete
Nov 14 '05 #7
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:

[...]
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:

(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)

CHAR_BIT isn't part of it.


Assuming that the input bytes are 8-bit bytes.


No, assuming they have values in the range 0..255.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #8
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:

pete <pf*****@mindspring.com> writes:
[...]
> A portable expression to make a long unsigned value of
> 0x645F8E3D, out of those byte values, is:
>
> (((long unsigned)byte3 << 24)
> + ((long unsigned)byte4 << 16)
> + (( unsigned)byte1 << 8)
> + byte2)
>
> CHAR_BIT isn't part of it.


Assuming that the input bytes are 8-bit bytes.


Absolutely not.
There is no value of CHAR_BIT which could cause the value
of above said expression, to equal anything other than 0x645F8E3DLU
when the values below are used in the expression.
0x8E byte1
0x3D byte2
0x64 byte3
0x5F byte4


You're right, my mistake. I paid insufficient attention to the
original question, which asked specifically about constructing the
value 0x645F8E3D from the input bytes 0x8E, 0x3D, 0x64, and 0x5F.

If I wanted to do this kind of thing on a system with CHAR_BIT != 8
I'd probably want to re-examine the underlying assumptions, but the
problem as stated didn't actually make any assumptions about CHAR_BIT
(though it did implicitly assume that the bytes are either unsigned or
wider than 8 bits).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
JC
Since you got the four received bytes:

unsigned long l;
char *p;

p = (char *)&l;
*p = byte_2; /* 0x3d, Assuming little endian */
*(p+1) = byte_1; /* 0x8e */
*(p+2) = byte_4; /* ox5f */
*(p+3) = byte_3; /* ox64 */

Now you should contains 0x645f8e3d; well, you get the idea...

"CBFalconer" <cb********@yahoo.com> wrote in message
news:41**************@yahoo.com...
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:

[...]
A portable expression to make a long unsigned value of
0x645F8E3D, out of those byte values, is:

(((long unsigned)byte3 << 24)
+ ((long unsigned)byte4 << 16)
+ (( unsigned)byte1 << 8)
+ byte2)

CHAR_BIT isn't part of it.


Assuming that the input bytes are 8-bit bytes.


No, assuming they have values in the range 0..255.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #10
"JC" <je*******@comcast.net> writes:
Since you got the four received bytes:

unsigned long l;
char *p;

p = (char *)&l;
*p = byte_2; /* 0x3d, Assuming little endian */
*(p+1) = byte_1; /* 0x8e */
*(p+2) = byte_4; /* ox5f */
*(p+3) = byte_3; /* ox64 */

Now you should contains 0x645f8e3d; well, you get the idea...


JC, please don't top-post. Your response should follow the quoted
material to which you're responding. See most of the articles in this
newsgroup for examples.

Your solution makes several unnecessary assumptions about the
representation of unsigned long; you assume that it's little endian,
and that it's exactly 4 bytes wide (if it's wider, you're leaving part
of l uninitialized). Your solution could also break if unsigned long
has padding bits or if plain char is signed. pete already posted (and
you quoted) a solution that doesn't make any of these assumptions.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
Keith Thompson wrote:

"JC" <je*******@comcast.net> writes:
Since you got the four received bytes:

unsigned long l;
char *p;

p = (char *)&l;
*p = byte_2; /* 0x3d, Assuming little endian */
*(p+1) = byte_1; /* 0x8e */
*(p+2) = byte_4; /* ox5f */
*(p+3) = byte_3; /* ox64 */

Now you should contains 0x645f8e3d; well, you get the idea...


JC, please don't top-post. Your response should follow the quoted
material to which you're responding. See most of the articles in this
newsgroup for examples.

Your solution makes several unnecessary assumptions about the
representation of unsigned long; you assume that it's little endian,
and that it's exactly 4 bytes wide (if it's wider, you're leaving part
of l uninitialized). Your solution could also break if unsigned long
has padding bits or if plain char is signed. pete already posted (and
you quoted) a solution that doesn't make any of these assumptions.


His solution also assumes 8 bit bytes. If CHAR_BIT were 16,
then there would be pairs of 0's sprinkled throughout
the hexadecimal representation of that result.

--
pete
Nov 14 '05 #12

JC wrote
p = (char *)&l;
*p = byte_2; /* 0x3d, Assuming little endian */
*(p+1) = byte_1; /* 0x8e */
*(p+2) = byte_4; /* ox5f */
*(p+3) = byte_3; /* ox64 */

Now you should contains 0x645f8e3d; well, you get the idea...
Keith wrote:
Your solution makes several unnecessary assumptions about the
representation of unsigned long; you assume that it's little endian,
and that it's exactly 4 bytes wide (if it's wider, you're leaving part
of l uninitialized). Your solution could also break if unsigned long
has padding bits or if plain char is signed. pete already posted (and
you quoted) a solution that doesn't make any of these assumptions.
Pete wrote:
His solution also assumes 8 bit bytes. If CHAR_BIT were 16,
then there would be pairs of 0's sprinkled throughout
the hexadecimal representation of that result.


I've been following this thread, and I still don't get it. Exactly how is
it that Pete's soln isn't married to 8 bit bytes when his left shifts were
hard coded as multiples of eight? MPJ
Nov 14 '05 #13
"Merrill & Michele" <be********@comcast.net> writes:
> > JC wrote
> > p = (char *)&l;
> > *p = byte_2; /* 0x3d, Assuming little endian */
> > *(p+1) = byte_1; /* 0x8e */
> > *(p+2) = byte_4; /* ox5f */
> > *(p+3) = byte_3; /* ox64 */
> >
> > Now you should contains 0x645f8e3d; well, you get the idea... >Keith wrote:
> Your solution makes several unnecessary assumptions about the
> representation of unsigned long; you assume that it's little endian,
> and that it's exactly 4 bytes wide (if it's wider, you're leaving part
> of l uninitialized). Your solution could also break if unsigned long
> has padding bits or if plain char is signed. pete already posted (and
> you quoted) a solution that doesn't make any of these assumptions.

Pete wrote:
His solution also assumes 8 bit bytes. If CHAR_BIT were 16,
then there would be pairs of 0's sprinkled throughout
the hexadecimal representation of that result.


I've been following this thread, and I still don't get it. Exactly how is
it that Pete's soln isn't married to 8 bit bytes when his left shifts were
hard coded as multiples of eight? MPJ


The original question was:

] I received byte by byte
] 0x8E byte1
] 0x3D byte2
]
] 0x64 byte3
] 0x5F byte4
]
] How can I construct to be LONG 0x645F8E3D (shift left >> 16)

The input values are treated as octets (which may or may not be
bytes).

Ignoring the "(shift left >> 16)" (I'm not sure what it means), the
requirement is to shove the specified values into 8-bit chunks of an
unsigned long. Pete's solution accomplishes that even if CHAR_BIT!=8.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14
"Merrill & Michele" <be********@comcast.net> wrote:

I've been following this thread, and I still don't get it. Exactly how is
it that Pete's soln isn't married to 8 bit bytes when his left shifts were
hard coded as multiples of eight? MPJ


The original question has nothing to do with bytes.
Consider this analogous question:

"How do I get (in decimal) 12345678 from the values
12, 34, 56, 78" ?

The answer is: 12 * 10^6 + 34 * 10^4 + 56 * 10^2 + 78
(where ^ means "power of").

Nothing to do with byte units. Similarly in hexadecimal:

0x645F8E3D == 0x64 * 16^6 + 0x5F * 16^4 + 0x8E * 16^2 + 0x3D

This is exactly analogous to the decimal example -- no mention
of byte units here either.
Note that each hex nibble is 4 bits, so multiplying by 16
is the same as left-shifting by 4 bits (regardless of the
byte size).
Nov 14 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
3
by: Dennis | last post by:
If I have a byte how would I port code from C++ to manipulate the byte array as if it were a long? For example: // C++ // assume the following // unsigned char* m_pPalette; // unsigned...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
4
by: Frederick Gotham | last post by:
What do you think of the following code for setting and retrieving the value of bytes in an unsigned integer? The least significant bit has index 0, then the next least significant bit has index 1,...
5
jeffbroodwar
by: jeffbroodwar | last post by:
Hi everyone, I have a program that converts variables long,string,double to byte array here's the code : for long : //CompanyId temp = longToByteArray(CompanyId); ...
10
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.