unsigned char to a char* conversion ?

unsigned char to a char* conversion ?

Post by Peter Marve » Wed, 28 Feb 2001 00:24:03



Hi,
        I'm curently working on my final year project for my degree and
have come up against a problem i can't see a way round. I am reading in an
"unsigned char" from the serial port and need to convert it to a "char*"
type string for processing by another part of the program. Can anyone
suggest how I go about this ???
                thanks for any assistance,

                Pete

 
 
 

unsigned char to a char* conversion ?

Post by Lew Pitch » Wed, 28 Feb 2001 00:45:15


On Mon, 26 Feb 2001 15:24:03 +0000, Peter Marven


>Hi,
>    I'm curently working on my final year project for my degree and
>have come up against a problem i can't see a way round. I am reading in an
>"unsigned char" from the serial port and need to convert it to a "char*"
>type string for processing by another part of the program. Can anyone
>suggest how I go about this ???

Given...

  unsigned char input_char;
  unsigned char output_buffer[2];
  unsigned char *pointer;

Then (assuming that you want a C string)...

  output_buffer[0] = input_char;
  output_buffer[1] = 0;
  pointer = &output_buffer[0];

or

  output_buffer[0] = input_char;
  output_buffer[1] = 0;
  pointer = output_buffer;

However, if you just wanted a pointer (not necessarily a string), then

  pointer = &input_char;

Quote:>            thanks for any assistance,

>            Pete

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group


(Opinions expressed are my own, not my employer's.)

 
 
 

unsigned char to a char* conversion ?

Post by Jem » Fri, 02 Mar 2001 21:58:43



says...

> On Mon, 26 Feb 2001 15:24:03 +0000, Peter Marven

> >Hi,
> >       I'm curently working on my final year project for my degree and
> >have come up against a problem i can't see a way round. I am reading in an
> >"unsigned char" from the serial port and need to convert it to a "char*"
> >type string for processing by another part of the program. Can anyone
> >suggest how I go about this ???

> Given...

>   unsigned char input_char;
>   unsigned char output_buffer[2];
>   unsigned char *pointer;

> Then (assuming that you want a C string)...

>   output_buffer[0] = input_char;
>   output_buffer[1] = 0;
>   pointer = &output_buffer[0];

> or

>   output_buffer[0] = input_char;
>   output_buffer[1] = 0;
>   pointer = output_buffer;

> However, if you just wanted a pointer (not necessarily a string), then

>   pointer = &input_char;

> >               thanks for any assistance,

> >               Pete

> Lew Pitcherz
> Information Technology Consultant
> Toronto Dominion Bank Financial Group


> (Opinions expressed are my own, not my employer's.)

Errr....Didn't you mean:
        unsigned char input_char;
        unsigned char output_buffer[2];
        char *pointer;

        Then (assuming that you want a C string)...

        output_buffer[0] = input_char;
        output_buffer[1] = 0;
        pointer = (char *)&output_buffer[0];

        or

        output_buffer[0] = input_char;
        output_buffer[1] = 0;
        pointer = (char *)output_buffer;

??
Otherwise you seem to be converting from unsigned char to unsigned
char... not very useful...

Or simply
        unsigned char input_char;
        char output_char;

        output_char = (char)input_char;

(Or am I missing / misreading something?)

Jem

 
 
 

1. Sparcworks C/C++: unsigned char vs signed char

Does anyone know of a way I can tell the Sparcworks compiler to make all
'chars' unsigned instead of signed (like the -funsigned-char flag under
the GNU C compiler)? I have a whole bunch of code that already exists that
expect char's to be unsigned but just declare variables as 'char'. Else
is there a portable way of using typedef's + #defines to trick the compiler?

thanks,
mahesh

2. Logitech mouse ?

3. Sun Workshop C Compiler ("char" as "unsigned char") ?

4. More on Brian K. West...

5. pointer of 'unsigned char' == 'char'!?

6. skip on windows NT

7. [2.5] const char* to char* conversion in console.h

8. Installing Opera 5.05 for 4.5 release

9. raw char * buffer to char * printable string conversion issue

10. [2.4] const char* to char* conversion in console.h

11. char *strcasestr(char *haystack, char *needle) a simple case independent strstr()

12. Possible bug in cc involving signed/unsigned chars

13. char * in network byte order to unsigned long int