Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Convert binary char array to integer with reordering

Reply
Thread Tools

Convert binary char array to integer with reordering

 
 
Jordan Abel
Guest
Posts: n/a
 
      10-25-2005
On 2005-10-25, Kenny McCormack <(E-Mail Removed)> wrote:
> In article <djljr4$cuq$(E-Mail Removed)>,
> Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote:
>>In article <(E-Mail Removed)>,
>>Jordan Abel <(E-Mail Removed)> wrote:
>>>int16_t A = data[1]<<8+data[0];
>>>int16_t B = data[3]<<8+data[2];

>>
>>>looks portable to me.

>>
>>int16_t does not exist in C89, and in C99 it is optional.
>>It simply doesn't exist on C99 systems that have (say) 18 bit ints.
>>That makes it standardized but not portable.

>
> Exactly.


which i call "portable enough" - guaranteed not to appear to work on systems
where it won't work.

but, if you must, here's the ruthlessly portable version:

assumptions: unsigned char data[2] contains the high 8 bits and then the low 8
bits of a signed 16-bit integer with the same sign representation as the host,
regardless of the actual byte size or integer width of the host.

int x =
((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
| data[0] /* low 'byte' */
| ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
/* that monster uses the system's sign extension to put the sign bit in the
* right place, and properly extend it on 2s-comp or 1s-comp systems. */
;

There may be superfluous parentheses - I never could remember the order of
shifting vs bitwise and, and i'm irrationally uncomfortable with the cast
operator

on a signed-magnitude 36-bit system with nine-bit bytes, this should convert
the bytes: 010011010 011001001

to the value
100000000000000000000001101011001001

or -6857 decimal.

I didn't bother with systems with a char of less than 8 bits since that's not
allowed by the standard.
 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      10-25-2005
On 2005-10-25, Jordan Abel <(E-Mail Removed)> wrote:
> int x =
> ((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
> | data[0] /* low 'byte' */
> | ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
> /* that monster uses the system's sign extension to put the sign bit in the
> * right place, and properly extend it on 2s-comp or 1s-comp systems. */

/* oops - forgot */
| (~0<-1?(data[1]&0200U)?(~0)<<15:0:0)
/* can anyone guess what that one does? */
> ;

 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      10-25-2005
On 2005-10-25, Jordan Abel <(E-Mail Removed)> wrote:
> int x =
> ((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
> | data[0] /* low 'byte' */
> | ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
> /* that monster uses the system's sign extension to put the sign bit in the
> * right place, and properly extend it on 2s-comp or 1s-comp systems. */

/* oops - forgot */
| (~0>=-1?(data[1]&0200U)?(~0)<<15:0:0)
/* can anyone guess what that one does? */
> ;

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-25-2005
Jordan Abel said:

> On 2005-10-25, Jordan Abel <(E-Mail Removed)> wrote:
>> int x =
>> ((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
>> | data[0] /* low 'byte' */
>> | ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
>> /* that monster uses the system's sign extension to put the sign bit
>> in the
>> * right place, and properly extend it on 2s-comp or 1s-comp systems.
>> */

> /* oops - forgot */
> | (~0>=-1?(data[1]&0200U)?(~0)<<15:0:0)
> /* can anyone guess what that one does? */
>> ;


Gives a possible trap representation on ones' comp systems?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-25-2005
In article <djlqrn$oq7$(E-Mail Removed)-infra.bt.com>,
Richard Heathfield <(E-Mail Removed)> wrote:
>Jordan Abel said:
>
>> On 2005-10-25, Jordan Abel <(E-Mail Removed)> wrote:
>>> int x =
>>> ((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
>>> | data[0] /* low 'byte' */
>>> | ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
>>> /* that monster uses the system's sign extension to put the sign bit
>>> in the
>>> * right place, and properly extend it on 2s-comp or 1s-comp systems.
>>> */

>> /* oops - forgot */
>> | (~0>=-1?(data[1]&0200U)?(~0)<<15:0:0)
>> /* can anyone guess what that one does? */
>>> ;

>
>Gives a possible trap representation on ones' comp systems?


Exactly.

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-25-2005
On 2005-10-25, Richard Heathfield <(E-Mail Removed)> wrote:
> Jordan Abel said:
>
>> On 2005-10-25, Jordan Abel <(E-Mail Removed)> wrote:
>>> int x =
>>> ((data[1] & 0177U) << /* non-sign-bit portion of high 'byte' */
>>> | data[0] /* low 'byte' */
>>> | ((~0U<<15)&((int)(signed char)( (data[1] & 0200U) << (CHAR_BIT- ))
>>> /* that monster uses the system's sign extension to put the sign bit
>>> in the
>>> * right place, and properly extend it on 2s-comp or 1s-comp systems.
>>> */

>> /* oops - forgot */
>> | (~0>=-1?(data[1]&0200U)?(~0)<<15:0:0)
>> /* can anyone guess what that one does? */
>>> ;

>
> Gives a possible trap representation on ones' comp systems?


~1>=-2, as i just _told_ you on ##c that i'd modify that to if challenged on
this basis
 
Reply With Quote
 
Dick de Boer
Guest
Posts: n/a
 
      10-26-2005

"Default User" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dick de Boer wrote:
>
> <cut top-post.
>
> Please don't top-post. Your replies belong following or (preferably)
> interspersed with properly trimmed quotes.
>

Sorry, slip of my finger (mind)

Dick


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char->integer, integer->char commands christophergraber@gmail.com Perl Misc 5 11-22-2005 07:13 AM



Advertisments