www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/25/15:36:49

Message-ID: <001501bf6710$21cbb840$0307028a@prmivv03>
From: "Petr Maxa" <maxa AT sse-za DOT sk>
To: <djgpp AT delorie DOT com>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 1000124164351 DOT 12997D-100000 AT is>
Subject: Re: signed - unsigned chars
Date: Tue, 25 Jan 2000 09:42:34 +0100
Organization: SSE s.p. Zilina
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Reply-To: djgpp AT delorie DOT com

On Mon, 24 Jan 2000, Eli Zaretskii wrote:

:
: On Mon, 24 Jan 2000, Petr Maxa wrote:
:
: > The problem is solved, but is not as neat as I would expect, because I
still
: > have to mix signed and unsigned types when I use libc functions like
printf,
: > sprintf in the case, that the unsigned type is readable text and need to
be
: > printed or when the buffers may use both text and binary (unsigned)
: > information.
:
: Why did you need to mix signed and unsigned when using library
: functions?  Are there any specific problems?  There shouldn't be:
: AFAIK, the library functions are 8-bit clean.  If you know about
: any problems, please report them here.

That is ok, I do not need 8-bit for libc functions, but we use data in our
buffers which are unsigned and sometimes these data include printable data,
which we process by the libc functions. For simple example we have something
like this:

#include <string.h>
#include <stdio.h>

typedef unsigned char uchar;

void code_message(uchar *buf,uchar code,uchar *message,uchar len)
{
 buf[0] = 0xff;       /* some kind of header */
 buf[1] = code;       /* type of data -> user message*/
 buf[2] = len;        /* data length */
 memmove(buf+3,message,len);
 buf[3+len] = code;   /* test character or sum of the message */
}

int main()
{
uchar buf[256];

  code_message(buf,0x81,(uchar *)"Hello world\n",13); /* 1-st overtyping */

  if((buf[0]==0xff) && (buf[1]==0x81))  /*!! if we use signed char, this
comparation would be useless !! */
    printf((char*)buf+3);       /* 2-nd overtyping */
 return 0;
}

As You can see because we use all 8 bits in char type we declare it as an
unsigned type. This code works, but needs two overtyping and it is due to
that
1. compiler treats included text "Hello world\n" as a "const char *" type
and we pass it to general function code_message, which process it .
2. printf function request the "signed char *" type as the first parameter.

For curiosity (or off topic), there is also another problem. If You have
signed char type variable with value 0xff (-1) and You convert it to int,
You get 0xffffffff that is also -1, but in our case it might produce buggy
code. For example, if we change the main function to this:

int main()
{
char buf[256];
int code1,code2;

  code_message((uchar*)buf,0x81,"Hello world\n",13); /* 1-st overtyping */

code1 = buf[0];   // 0xff to 0xffffffff
code2 = buf[1];   // 0x81 to 0xffffff81
  if((code1==0xff) && (code2==0x81))  // so the test will be false
    printf(buf+3);
return 0;
}





- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019