unsigned char和char的区别在哪里? [英] Where is the difference of unsigned char and char?

查看:92
本文介绍了unsigned char和char的区别在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned char和char的区别在哪里?如何将char arrary更改为未签名的char arrary,如下所示:

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];

推荐答案

取决于它们的用途.如果用于存储字符串数据,则没有.如果用于数学,则有很多.

字符为8位,这将为您提供256个不同的值.未签名的字符必须为正.在这种情况下,这256个不同的值从0-255映射.

但是,使用带符号的字符时,我们可以同时使用正数和负数.由于我们希望正数值与负数值一样多,因此我们移动"了表示的范围,使其从-127..128开始.


由于您的代码将其用作字符串,因此您实际上无需执行任何操作-您只需要告诉编译器将其视为无符号即可.
从您的问题来看,您似乎有一些声明为char *的东西,而代码的另一部分则需要/期望一个未签名的char *

因此,您可以投放东西.

char * someText ="abcdefg1234567890 \ n \ r";
printf(%s",(unsigned char *)someText);

另外,您也可以使用strdup或3个函数strlen/malloc/strcpy(strdup内部执行)创建字符串的副本.


也许这段代码将有助于解释一下?

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)


Perhaps this code will help explain it a bit?

#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) ☻


在C/C ++中charsigned charunsigned char是3种不同的类型,但是它们的二进制表示形式是相同的(8位整数).作为证明,您可以编写3个带有重载的函数,它将进行编译:
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:
void f(char c) {}
void f(signed char c) {}
void f(unsigned char c) {}


signed charunsigned char的用法是存储二进制数据(整数),而char的用途是存储文本字符(!!!).所以char类型是有符号的还是无符号的???大多数编译器将其视为带符号的-我猜是因为向后兼容,并且使char的行为与其他整数类型相似(例如,当您仅编写int时,其行为与signed int相同).

但是,对于char类型,大多数编译器允许您指定将char视为有符号还是无符号.

在使用模板魔术的情况下,有时您必须对所有3种类型都实现重载或专业化,因为模板对精确的类型匹配不敏感.在很多其他情况下,charsigned char(或者unsigned char,如果您告诉编译器将char视为无符号的)则可以互换(自动转换).


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).


In惯例,当您看到char时,通常意味着此变量包含一串ascii字符:
In practices, when you see char, it usually means that this variable holds a string of ascii characters:
char *message = "I'm an ascii string";


但是unsigned char通常是二进制数据数组,而不是字符串:


but unsigned char, is usually an array of binary data, not a sting:

unsigned char gif_header_magic[] = {0x47, 0x49, 0x46, 0x38, 0x39, 0x61}


在这种情况下,您不应该使用printf直接打印它,或者使用某些字符串比较功能,例如strcmp.相反,您应该将其打印为十六进制,并且为了进行比较使用memcmp,当存在一些无法打印的字符或0x00时,您将遇到问题.


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.


这篇关于unsigned char和char的区别在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆