473,804 Members | 2,028 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
14 2981
"JC" <je*******@comc ast.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_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 #11
Keith Thompson wrote:

"JC" <je*******@comc ast.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********@com cast.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_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 #14
"Merrill & Michele" <be********@com cast.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
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
4164
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
22
3727
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
2138
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
2788
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
16149
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
4904
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
10595
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
10343
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
10335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7633
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
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.