473,386 Members | 1,766 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,386 software developers and data experts.

byte to integer

Vtd
Hi All,
Simple question regarding byte to integer conversion:
integers are 32, char is 8 bits.

unsigned int a;
int b;
char c;
....
a = (unsigned int)c; /* c is 0 extended to integer (upper 24 bits of a
filled with 0's). Correct? */

b = (int)c; /* c is sign extended to integer (upper 24 bits of b are
filled with 1's or 0's depending on bit 7 of c). Correct? */

Thanks,
Vlad

Nov 15 '05 #1
5 3164
In article <11**********************@g49g2000cwa.googlegroups .com>,
Vtd <vt*@charter.net> wrote:
Simple question regarding byte to integer conversion:
integers are 32, char is 8 bits.
[Not in general, but I will assume you are discussing a sample
implementation.]

unsigned int a;
int b;
char c; a = (unsigned int)c; /* c is 0 extended to integer (upper 24 bits of a
filled with 0's). Correct? */ b = (int)c; /* c is sign extended to integer (upper 24 bits of b are
filled with 1's or 0's depending on bit 7 of c). Correct? */


Not exactly. Whether 'char' is signed or unsigned is implementation
dependant. If it is unsigned, then because all the possible values
would fit within an int of the width you indicated for the implementation,
then the value would NOT have sign bit propagation.
--
Entropy is the logarithm of probability -- Boltzmann
Nov 15 '05 #2
Vtd
Thanks Walter. So how can I sign extend char to integer?

Nov 15 '05 #3


Vtd wrote:
Hi All,
Simple question regarding byte to integer conversion:
integers are 32, char is 8 bits.

unsigned int a;
int b;
char c;
...
a = (unsigned int)c; /* c is 0 extended to integer (upper 24 bits of a
filled with 0's). Correct? */
It depends on the value of `c'.

If `c' is non-negative (which will always be the case if
the implementation uses an unsigned `char'), converting this
non-negative value to `unsigned' will produce a small result
with a lot of high-order zero bits. If `c' is negative (which
can only happen if the implementation uses a signed `char'),
converting this negative value to `unsigned' will produce a
large result with a lot of high-order one bits.

Note that the cast has no effect; `a = c' would behave
exactly the same way.
b = (int)c; /* c is sign extended to integer (upper 24 bits of b are
filled with 1's or 0's depending on bit 7 of c). Correct? */


`b' will be set to the same numerical value as `c', which
will always be non-negative on an implementation with unsigned
`char' and could be either negative or non-negative on an
implementation with signed `char'.

Once again, the cast has no effect; `b = c' would behave
exactly the same way.

I imagine your next question might be "Well then, how do
I zero- or sign-extend a `char'?" The pedantic answer is
"It's difficult," but the practical answer is "It's easy:"

a = (unsigned char)c;
b = (signed char)c;

The analysis for systems that use two's complement for
negative `signed char' values is easy; I'll leave it to you.
The pedant's quibble arises because ones' complement and
signed-magnitude representations are also allowed, and these
make things tricker. It's strange to refer to the first line
above as "zero-extending" in a case where `(unsigned char)c'
may have a different bit pattern than plain `c'. The second
line is even more problematic: if `char' is unsigned and the
value of `c' is too large for a `signed char', the conversion
produces undefined behavior. However, such systems seem to
be quite rare nowadays -- hence the practical "It's easy."

--
Er*********@sun.com

Nov 15 '05 #4
"Vtd" <vt*@charter.net> writes:
Thanks Walter. So how can I sign extend char to integer?


Please provide some context when you post a followup. If you've been
following this newsgroup, you've seen the following many times; please
pay attention to it:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

If you want sign extension, use signed char.

For example:

unsigned char uc = -1; /* value is UCHAR_MAX, typically 255 */
signed char sc = -1; /* value is -1 */
char c = -1; /* value could be either UCHAR_MAX or -1 */

If it matters whether a character type is signed or unsigned, don't
use "plain" char; use signed char or unsigned char explicitly.

--
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 15 '05 #5
On Tue, 12 Jul 2005 18:50:28 +0000, Walter Roberson wrote:
In article <11**********************@g49g2000cwa.googlegroups .com>,
Vtd <vt*@charter.net> wrote:
Simple question regarding byte to integer conversion:
integers are 32, char is 8 bits.
[Not in general, but I will assume you are discussing a sample
implementation.]

unsigned int a;
int b;
char c;

a = (unsigned int)c; /* c is 0 extended to integer (upper 24 bits of a
filled with 0's). Correct? */


Not if char has a signed representation. Note that conversions between
integer types are NOT defined in terms of "sign extension" which is a
definition based on representation, they are defined in terms of value. So
if a char with a value of -1 is converted to an int the result remains -1
irrespective of how that is represented. Obviously unsigned types can't
represent the value -1, so unsigned types have a special rule to determine
what the result will be. The rule is that you add or subtract one more
than the maximum value the unsigned type can represent as many times as is
necessary to give a value it can represent (it is essentially a modulo
reduction). For the case of -1 you just add one more than the maximum
value i.e. -1 + (UINT_MAX+1) which gives UINT_MAX as the result. On
2's complement systems this looks a lot like sign extension at the
representation level. Essentially you'll get sign extension on 2's
complement systems on converting to a wider type when the SOURCE type is
signed, the target type is not important.
b = (int)c; /* c is sign extended to integer (upper 24 bits of b are
filled with 1's or 0's depending on bit 7 of c). Correct? */


Not exactly. Whether 'char' is signed or unsigned is implementation
dependant. If it is unsigned, then because all the possible values would
fit within an int of the width you indicated for the implementation,
then the value would NOT have sign bit propagation.


Right - it makes little sense to talk about sign bit propagation from an
unsigned type i.e. a type that have no sign bit.

Lawrence
Nov 15 '05 #6

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

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
235
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...
8
by: steve | last post by:
I'd like to take the value of an int and put the value into a byte array. Basically the hexidecimal representation of an integer, with out having to convert the integer to a string and then to a...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
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)...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
11
by: cmdolcet69 | last post by:
Public Shared adc_value As word_byte Public Class byte_low_hi Public low_byte As Byte Public high_byte As Byte End Class pmsg(2) = CChar(adc_value.word16.high_byte)
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.