473,703 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert form byte[4] to Int32 while retaining the binary value of the byte array

how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array

Jul 22 '05 #1
19 4142
je**@foundrymus ic.com wrote:
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


First of all, you have to be a bit more specific. For example, you need
to say that your 'byte' has only 8 bits and your Int32 is (probably) 32
bits long. Second, what does "binary value" mean? Is your 'byte' type
signed or unsigned? Third, what do you mean by "convert"? An array of
four chars (signed or unsigned) cannot be _converted_ to a single int in
C++ sense, so you probably mean "how to form a 32-bit value out of 4 8-bit
values and retain the bit pattern of each of 4 source values?" That can
be answered but you need to specify where your 'byte[0]' should go, to the
beginning or to the end of the resulting integer.

See what I mean? Perhaps while trying to understand what you need, you
will find a decent way to "convert" what you need into what you desire
using << and | operators...

What are you trying to accomplish, anyway?

V
Jul 22 '05 #2

<je**@foundrymu sic.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


As far as I know, neither "Int32" nor "byte" are defined as types in C++. I
can *guess* how they're defined, but it's only an eductated guess. What do
you mean when you say you want to "retain the binary value"? And in what
way do you want to "convert" the value?

If what you have is an array of 4 8-bit unsigned char values, and you want
to interpret them as a 32-bit signed long, then one way might be to use a
pointer, like this:

unsigned char array[4];
//.. fill that array somehow...

long* pInt32 = (long*)(&array[0]);

Now, *pInt32 will be interpreted as a signed long.

BUT!!! This may not work on your machine! For one thing, the byte ordering
of the bytes in the array may not be correct for an integer representation
on your machine. On a PC, the ordering of bytes is opposite what it is on
the Mac, for example. It all depends upon you KNOWING that the values in
that array are in the correct order in the first place.

(On the other hand, if you know that they're in the OPPOSITE order, you can
always copy them to another array in reverse order, and then do the above!)

There may be other issues, possibly, such as the size of a char and a long
on your machine, and the possibility that you could put some bit pattern
into that array which would not be a valid 32-bit long integer.

In general, it's best to avoid doing this if possible. But, you can try it
and see if it works for you. Remember, though, that the solution likely
won't be portable across machines.

-Howard

Jul 22 '05 #3
Howard wrote:
<je**@foundrymu sic.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


As far as I know, neither "Int32" nor "byte" are defined as types in C++. I
can *guess* how they're defined, but it's only an eductated guess. What do
you mean when you say you want to "retain the binary value"? And in what
way do you want to "convert" the value?

If what you have is an array of 4 8-bit unsigned char values, and you want
to interpret them as a 32-bit signed long, then one way might be to use a
pointer, like this:


<snip..>

Int32 convert(Byte b[4])
{
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
}

Byte b[4] = { 1, 2, 3, 4 };

Int32 i = convert(b);
Let's not get _that_ picky, people.
Jonathan
Jul 22 '05 #4
Jonathan Mcdougall wrote:
Howard wrote:
<je**@foundrymu sic.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


As far as I know, neither "Int32" nor "byte" are defined as types in
C++. I can *guess* how they're defined, but it's only an eductated
guess. What do you mean when you say you want to "retain the binary
value"? And in what way do you want to "convert" the value?

If what you have is an array of 4 8-bit unsigned char values, and you
want to interpret them as a 32-bit signed long, then one way might be
to use a pointer, like this:

<snip..>

Int32 convert(Byte b[4])
{
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
}

Byte b[4] = { 1, 2, 3, 4 };

Int32 i = convert(b);
Let's not get _that_ picky, people.


How did you know the OP didn't want

return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);

???

V
Jul 22 '05 #5

<je**@foundrymu sic.com> wrote in message news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


Your question has nothing to do with C++.
(hint: experiment with a union of these two types)

Jul 22 '05 #6

"Jonathan Mcdougall" <jo************ ***@DELyahoo.ca > wrote in message
news:o0******** ************@we ber.videotron.n et...
Howard wrote:
<je**@foundrymu sic.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array


As far as I know, neither "Int32" nor "byte" are defined as types in C++.
I can *guess* how they're defined, but it's only an eductated guess.
What do you mean when you say you want to "retain the binary value"? And
in what way do you want to "convert" the value?

If what you have is an array of 4 8-bit unsigned char values, and you
want to interpret them as a 32-bit signed long, then one way might be to
use a pointer, like this:


<snip..>

Int32 convert(Byte b[4])
{
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
}

Byte b[4] = { 1, 2, 3, 4 };

Int32 i = convert(b);
Let's not get _that_ picky, people.


How do you know that the byte ordering is correct? Maybe b[0] is the LSB!

(And of course you're just assuming the types match. Logical assumption,
but not explicitly stated.)

-Howard
Jul 22 '05 #7
Alan wrote:
<je**@foundrymu sic.com> wrote in message news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array

Your question has nothing to do with C++.
(hint: experiment with a union of these two types)


What's a 'union' outside of C++ context? You are contradicting yourself.
If the OP has to look at the union, the question has _everything_ to do
with C++.

Besides, unions are not for conversions between types. They are only to
save memory when storing different types. See more discussions on that
in all known C++ newsgroups.

V
Jul 22 '05 #8

"Victor Bazarov" <v.********@com Acast.net> wrote in message news:Cf******** *********@newsr ead1.dllstx09.u s.to.verio.net. ..
Alan wrote:
<je**@foundrymu sic.com> wrote in message news:11******** *************@f 14g2000cwb.goog legroups.com...
how do you convert form byte[4] to Int32 while retaining the binary
value of the byte array

Your question has nothing to do with C++.
(hint: experiment with a union of these two types)


What's a 'union' outside of C++ context? You are contradicting yourself.


Hardly, they are also used in other languages (as you know), e.g. C
If the OP has to look at the union, the question has _everything_ to do
with C++.
see above
Besides, unions are not for conversions between types.


...but they can be used for that in C
Jul 22 '05 #9

"Alan" <al**@surfbest. net> wrote in message
news:10******** *****@news.supe rnews.com...

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:Cf******** *********@newsr ead1.dllstx09.u s.to.verio.net. ..
Alan wrote:
> <je**@foundrymu sic.com> wrote in message
> news:11******** *************@f 14g2000cwb.goog legroups.com...
>
>>how do you convert form byte[4] to Int32 while retaining the binary
>>value of the byte array
>
>
> Your question has nothing to do with C++.
> (hint: experiment with a union of these two types)


What's a 'union' outside of C++ context? You are contradicting yourself.


Hardly, they are also used in other languages (as you know), e.g. C
If the OP has to look at the union, the question has _everything_ to do
with C++.


see above
Besides, unions are not for conversions between types.


..but they can be used for that in C


The OP asked about doing something in C++ (assumed, since that's where he
posted). You answered in two parts. First, you said his question had
"nothing" to do with C++. Victor rightly asks "why?". In what way does a
question about how to accomplish a common task not a question about how to
accomplish that task in C++, given that that's what we discuss here? Then,
you suggest that somehow the ability to use a union construct to accomplish
the task should be a hint to him. We can only assume that you mean it
should be a hint that the original question has nothing to do with C++. Yet
the union construct does exist in C+, so in what way does it hint that the
OP's question is not about C++? You're suggesting one way to do what he
wants to do (albeit not a good way, IMO), with a construct that is available
in C++, yet saying that this somehow demonstrates that the question you're
proposing an answer to is therefore NOT related to C++. Are you as confused
as I am yet? :-)

-Howard

Jul 22 '05 #10

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

Similar topics

1
4482
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding(); now i am using enc.GetString(value) and the value retured is one byte less if the size of the byte array is Odd. In case of files having even number of bytes, the convertion is happening correctly and...
3
14501
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
4
79648
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
1
5770
by: Angel Filev | last post by:
Hi everyone, I am trying to store a file as a binary array in an "image" field in SQL Server 2000 database. It works OK except for the ".PDF" files, which I believe get corrupted in the process of reading a stream to a byte array. Uploading and downloading seems to work fine, but "Acrobat" pop ups "The file is damaged and could not be repaired" error. Is there any way I can make this to work.
2
4856
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of singles that correspond to those in my structure detailed below. So when I load the file, all that I know for certain is that there will be some multiple of these eight singles represented in the binary data. My code below will read the data...
4
7814
by: Sam | last post by:
Hi, I'm trying to convert a string to a binary format (byte) I've looked at BitConverter class, but I can't figure out how to use it for a Strin. Does anyone know ? Thx
14
7962
by: Me | last post by:
Hi all I am getting a really bizzare error on when I convert a string into a datetime: The code is : DateTime dt1 = Convert.ToDateTime("10 Sep 2005"); Console.WriteLine(dt1.Year);
10
4963
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
7
4271
by: mehafi | last post by:
hi, I need to write a few integer numbers to file. A method write from FileStream writes an array of bytes. So how to conwert my integers to byte array? I've tried Convert class, but it hasn't proper methods. I've tried also TypeDescriptor.GetConverter ... but got message "cant convert byte to byte" Could someone write me a line of code which will ilustrate this? thanks in advance
0
8758
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
9121
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
9017
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
8962
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...
0
7867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
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
4432
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
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3123
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

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.