<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>char to unsigned int</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/18009/char-to-unsigned-int</link><description> Hi all 
 
Here is some code 
 
char prev_ch; 
unsigned int chksum_read; 
 
chksum_read += (WORD)prev_ch*256; 
chksum_read += (unsigned int)prev_ch&amp;lt;&amp;lt;8; 
 
if prev_ch has a value of 0xDB after one of of the two above statments cheksum_read is 0xDA00 instead</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/86963?ContentTypeID=1</link><pubDate>Mon, 11 Oct 2004 00:16:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3ad13533-17b2-4560-bb02-5c43897fe396</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;Could you perhaps show the real code that you have.  That would make it a lot easier than talking in circles.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/42939?ContentTypeID=1</link><pubDate>Sun, 10 Oct 2004 23:27:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:5bad5b97-0fd8-4dec-95e9-9bd398089a94</guid><dc:creator>Rod Ladwig</dc:creator><description>&lt;p&gt;prev_ch is a unsigned char not char.&lt;br /&gt;
word is unsigned int&lt;br /&gt;
prev_ch is 0 before calculation.&lt;br /&gt;
&lt;br /&gt;
this is a strange one?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/42935?ContentTypeID=1</link><pubDate>Sat, 09 Oct 2004 23:37:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:2f527361-7a24-48fd-8f18-040960c82ccb</guid><dc:creator>Jon Ward</dc:creator><description>&lt;p&gt;What is the value of chksum_read before either of the 2 chksum_read += statements?&lt;br /&gt;
&lt;br /&gt;
Is it initialized to 0?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Jon&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/72779?ContentTypeID=1</link><pubDate>Sat, 09 Oct 2004 20:19:59 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:25d6ac3d-5f56-4f40-9f79-4902e94a1ac3</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;you should never trust the implicit signed-ness of the bare &amp;quot;char&amp;quot; declaration, since it varies from compiler to compiler&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
and many compilers provide a command-line option to change it!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/42936?ContentTypeID=1</link><pubDate>Sat, 09 Oct 2004 17:39:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1212eaa4-0c41-48e3-a5aa-87bad0ac519a</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;I second Andrew&amp;#39;s suggestion.  I&amp;#39;d add that you should never trust the implicit signed-ness of the bare &amp;quot;char&amp;quot; declaration, since it varies from compiler to compiler.  &amp;quot;U8&amp;quot; is also useful as it clearly makes the distinction between a variable intended as a small integer and a variable intended as human-readable text (a &amp;quot;&lt;b&gt;char&lt;/b&gt;acter&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
Do you mean that both forms of the statement posted produce the same result?  Or that one produces the expected result and one does not?  (If so, which one works?)&lt;br /&gt;
&lt;br /&gt;
The snippet as shown does not initialize chksum_read, so the += leaves you at the mercy of whatever garbage was in the variable.  If that happened to be 0xFF00, you would see the described results.&lt;br /&gt;
&lt;br /&gt;
Assuming we&amp;#39;re talking about the 8051, if you are going to do a lot of shift by 8s, you might consider a union type.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
typedef struct
    {
    U8 byte[2];
    U16 u16;
    } BytewiseU16;

U8 prev_ch;
Bytewise16 chksum_read;

// shift left by 8
chksum_read.byte[0] = chksum_read.byte[1];
chksum_read.byte[1] = 0;

// add prev_ch shifted left by 8
chksum_read[0] += prev_ch;
&lt;/pre&gt;
&lt;br /&gt;
It&amp;#39;s a little ugly and non-portable to different endian systems, but will save you some time and code space if that&amp;#39;s important.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: char to unsigned int</title><link>https://community.arm.com/thread/42937?ContentTypeID=1</link><pubDate>Sat, 09 Oct 2004 15:55:36 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6c24b080-6770-4de8-8eb2-b462cf84c21f</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;What is your definition of &lt;b&gt;WORD&lt;/b&gt;?&lt;br /&gt;
&lt;br /&gt;
As I&amp;#39;ve said many times before, WORD (and, hence, things like DWORD, etc) is a poor type name because it tells you neither its size nor its signed-ness.&lt;br /&gt;
It would be far better to choose a name that explicitly shows both.&lt;br /&gt;
&lt;br /&gt;
I use:&lt;br /&gt;
&lt;b&gt;U32&lt;/b&gt; - &lt;b&gt;U&lt;/b&gt;nsigned &lt;b&gt;32&lt;/b&gt; bits&lt;br /&gt;
&lt;b&gt;S32&lt;/b&gt; - &lt;b&gt;S&lt;/b&gt;igned &lt;b&gt;32&lt;/b&gt; bits&lt;br /&gt;
&lt;b&gt;U16&lt;/b&gt; - &lt;b&gt;U&lt;/b&gt;nsigned &lt;b&gt;16&lt;/b&gt; bits&lt;br /&gt;
&lt;b&gt;S16&lt;/b&gt; - &lt;b&gt;S&lt;/b&gt;igned &lt;b&gt;16&lt;/b&gt; bits&lt;br /&gt;
etc.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>