Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Where is the difference of unsigned char and char? How can I change a char arrary to unsigned char arrary like this:
char sPath[256]="abcdef1234567\r\t\n";
unsigned char sTemp[256];
Posted
Comments
Richard MacCutchan 28-Oct-12 8:07am    
Your question does not really make any sense, what are you trying to achieve?

Depends on what they're being used for. If it's for storing string data, there's none. If it's for maths, then there's quite a bit.

A char is 8 bits, this gives you 256 different values that it may have. Unsigned chars must be positive. In this case, these 256 different values get mapped from 0-255.

However, when using signed chars we can have both positive and negative numbers. Since we want to have as many positive values as negative ones, we 'shift' the range that's represented, such that it goes from -127..128.


Since your code is using it as a character string, you don't have to actually do anything to it - you just need to tell the compiler to treat it as unsigned.
From your question, it seems likely that you have something declared as being char*, while another part of the code needs/expects an unsigned char*

So, you could just cast the thing.
I.e
char *someText = "abcdefg1234567890\n\r";
printf("%s", (unsigned char*)someText);

Alternatively, you could just create a copy of the string, using either strdup or the 3 functions strlen/malloc/strcpy (which strdup does internally)


EDIT: Perhaps this code will help explain it a bit?

C#
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, n;
    char someData[] = { -2, -1, 0, 1, 2};
    n = 5;

    //1. Treat as a string of chars
    printf("String: '%s'\n\n", someData);

    // treat each char as an int
    for (i=0; i<n; i++)
        printf("(int) %d\n", (int)someData[i]);
    printf("\n");

    // treat each char as an unsigned int
    // sizeof(char) != sizeof(int) - that's why both unsigned char and unsigned int used for casting.
    for (i=0; i<n; i++)
        printf("(unsigned int) %d\n", (unsigned char)someData[i]);
    printf("\n");


    // treat each one as a char
    for (i=0; i<n; i++)
        printf("(char) %c\n", (char)someData[i]);
}




Output
String: '■ '

(int) -2
(int) -1
(int) 0
(int) 1
(int) 2

(unsigned int) 254
(unsigned int) 255
(unsigned int) 0
(unsigned int) 1
(unsigned int) 2

(char) ■
(char)
(char)
(char) ☺
(char) ☻
 
Share this answer
 
v2
Comments
CPallini 28-Oct-12 11:41am    
My 5. Owever the printf function is probably one of the worst candidate for the cast example. :-)
enhzflep 28-Oct-12 11:46am    
Thank-you CPallini. I'll see if I can't come up with a better example.
In C/C++ char, signed char, and unsigned char are 3 different types however their binary representation is the same (8 bit integer). As a proof you can write 3 functions with overloads, it will compile:
C++
void f(char c) {}
void f(signed char c) {}
void f(unsigned char c) {}

The usage of signed char and unsigned char is to store binary data (integers) while char is used to store a character of a text (!!!). So the char type signed or unsigned??? Most compilers treat it as signed - I guess because of backward compatibility and to make char behave similarly to other integral types (for example when you write simply int it behaves as signed int).

However in case of the char type most compilers allow you to specify whether to treat char as signed or unsigned.

In case of template magic sometimes you have to implement overloads or specializations for all 3 types because templates are touchy about exact type match. In a lot of other cases char and signed char (or unsigned char if you told the compiler to treat char as unsigned) are exchangeable (automatically casted).
 
Share this answer
 
In practices, when you see char, it usually means that this variable holds a string of ascii characters:
C++
char *message = "I'm an ascii string";

but unsigned char, is usually an array of binary data, not a sting:
C++
unsigned char gif_header_magic[] = {0x47, 0x49, 0x46, 0x38, 0x39, 0x61}

In such case, you shoudn't print it directly using printf or use some string comparison function like strcmp. Instead you should print it as hex and for comparing use memcmp, you will have problems when there is some unprintable character or 0x00.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900