简体   繁体   English

将char转换为int [C]

[英]Converting char to int [C]

I have a char byte that I want to convert to an int. 我有一个想要转换为int的char字节。 Basically I am getting the value (which is 0x13) from a file using the fopen command and storing it into a char buffer called buff. 基本上,我使用fopen命令从文件中获取值(0x13),并将其存储到称为buff的char缓冲区中。

I am doing the following: 我正在执行以下操作:

//assume buff[17] = 0x13

v->infoFrameSize = (int)buff[17] * ( 128^0 );

infoFrameSize is a type int that is stored in a structure called 'v'. infoFrameSize是一种类型int,存储在称为“ v”的结构中。

The value I get for v->infoFrameSize is 0x00000980. 我为v-> infoFrameSize获得的值为0x00000980。 Should this be 0x00000013? 应该是0x00000013吗?

I tried taking out the multiply by 128 ^ 0 and I get the correct output: 我尝试将乘以128 ^ 0取出,然后得到正确的输出:

v->infoFrameSize = 0x00000013

Any info or suggested reading material on what is happening here would be great. 关于这里发生的事情的任何信息或建议的阅读材料都很好。 Thanks! 谢谢!

^是按位异或运算,不是幂运算。

Operator ^ in C does bit operation - XOR. C中的运算符^执行位运算-XOR。 128 xor 0 equals 128. 128 xor 0等于128。

128^0 is not doing what you think it does. 128 ^ 0并未执行您认为的操作。

cout << (128^0)

returns 128. 返回128。

Try pow(128,0) . 尝试pow(128,0) Then, add the following to the top of your code: 然后,将以下内容添加到代码顶部:

#include <math.h>

Also, note that pow always returns a float. 另外,请注意pow总是返回浮点数。 So you'll need to cast your final answer to an int. 因此,您需要将最终答案转换为整数。 So: 所以:

(int)(buff[17] * pow(128,0));

In C 128 ^ 0 equates the bitwise XOR of 128 and 0, it doesn't raise 128 to the power of 0 (which is just 1). 在C 128 ^ 0等于128与0的按位XOR运算,它不会将128乘以0的幂(仅为1)。

A char is simply an integer consisting of a single byte, to "convert" it to an int (which isn't really converting, you're just storing the byte into a larger data type) you do: 一个char只是一个由单个字节组成的整数,可以将其“转换”为一个int (这并不是真正的转换,您只是将字节存储为更大的数据类型),可以这样做:

char c = 5;
int i = (int)c

tada. 多田

There is no point in the ^0 term. ^0项没有意义。 Anything xor'd with zero remains unchanged (so 128^0 is 128). 异或为零的任何内容均保持不变(因此128^0为128)。

The value you get is correct; 您得到的值是正确的; when you multiply 0x13 (aka 19) by 128 (aka 0x80), you get 0x0980 (aka 2432). 当您将0x13(aka 19)乘以128(aka 0x80)时,会得到0x0980(aka 2432)。

Why would you expect the assignment to ignore the multiplication? 您为什么期望赋值忽略乘法?

To convert a char to an int , you merely cast it: 要将char转换为int ,只需将其转换为:

char c = ...;
int x = (int) c;

K&R would have you read the one byte from the file using getc() and store it directly into an int which eliminates any issues you might be seeing. K&R会让您使用getc()从文件中读取一个字节并将其直接存储到int中,从而消除了可能会遇到的任何问题。 However, if you are reading from the file into an array of bytes, simply cast to int as follows: 但是,如果您正在从文件中读取字节数组,则只需将其强制转换为int,如下所示:

v->infoFrameSize = (int)buff[17];

I'm not sure why you're multiplying by 128^0. 我不确定为什么要乘以128 ^ 0。 The only problem I know of when converting from char to int is that char can be signed or unsigned, depending on the platform. char转换为int时,我知道的唯一问题是char可以是有符号的或无符号的,具体取决于平台。 If it happens to be signed, a big positive number stored inside a char may end up being considered as negative. 如果碰巧已签名,则存储在char中的大正数可能最终被视为负数。 When you will print it, it will either be a negative number or an abnormally big number (if you print it as an unsigned integer). 当您打印它时,它将是一个负数或一个异常大的数字(如果您将其打印为无符号整数)。 The solution is simply to use signed char or unsigned char explicitly in cases like this one. 解决方案就是在这种情况下显式使用带signed charunsigned char

"^" is a bitwise XOR Operation, if you want to do an exponent use 如果要进行指数使用,则“ ^”是按位异或运算

pow(128,0);

Why are you multiplying by one? 你为什么要乘一?

You can convert from a char to an int by simply defining an int and setting it like so: 您可以通过简单地定义一个int并将其设置如下,将char转换为int:

char x = 0x13;
int y;
y = (int)x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM