473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2980
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*****@mindsp ring.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_Keit h) 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*****@mindsp ring.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*****@mindsp ring.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

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

pete <pf*****@mindsp ring.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_Keit h) 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********@yah oo.com> wrote in message
news:41******** ******@yahoo.co m...
Keith Thompson wrote:
pete <pf*****@mindsp ring.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #10

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

Similar topics

9
3272
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 a={0x0,0x0,0xfe,0x30,0x9e,0x2,0x66,0,0} and the bytes 0xfe,0x30,0x9e,0x2 should be put into long variable which should then contain 43921662 how to achieve this?
19
4153
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
22
3726
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 wintel compilers support a 16-byte "long double" (with 33 decimal digits of accuracy) including proper printf() support. I found some compilers that did support "long double", but theirs was an 8-byte or 10-byte or 12-byte type with accuracy the...
289
1805
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 stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
3
2137
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 char* m_pData; //
6
2786
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) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
4
3398
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() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an error tells me something like "Additional information: Caractère de substitution faible trouvé...
4
2215
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, and so on. The code computes at runtime the byte-order of the unsigned integer, but alas it would be better if it could be determined at compile-time. The code potentially invokes undefined behaviour if an unsigned integer contains padding bits....
5
16148
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); for (i=0,i2=7; i<5; i++,i2--)
10
4903
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 performance in my case.
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.