-
int 与 unsigned int (uint) 比较时出现的问题
2012-07-24 18:24:49今天遇到一个问题 ,简单描述如下 ... int index = -1; NSMutableArray *arr = [NSMutableArray array]; [arr addObject:@"a"]; [arr addObject:@"b"]; NSLog(@"%d",[arr count]); if (index>[ar今天遇到一个问题 ,简单描述如下
int index = -1;
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:@"a"];
[arr addObject:@"b"];
NSLog(@"%d",[arr count]);
if (index>[arr count]) {
NSLog(@"所谓不可能的事出现了");
}else if (index==[arr count])
{
NSLog(@"这不可能");
}
else {
NSLog(@"应该如此啊");
}
我们可以看到 index = -1,[arr count] = 2 , 执行结果却打印的是 :所谓不可能的事出现了
思考下。。。。
[arr count] 返回的是 NSUInteger 也就是 unsigned int (uint) 类型 ,
当 int 与 unsigned int (uint) 比较时, unsigned int (uint) 向 int 转化, -1转成4294967295 那么在判断时 就出现了所谓的 -1>2
而 NSUInteger 也就是 unsigned int (uint) 向 int 转化时却不会有问题 因此 NSLog(@"%d",[arr count]); 打印出来的是2
备忘:
在使用 [NSarray count] 时要时刻谨记 返回的值是 NSUInteger 也就是 unsigned int (uint) 类型 。
所以下列的循环应该这么写
for (int i=-1; i<(int)[arr count]; i++) {
NSLog(@"%d",i);
}
-
unsigned int 转 unsigned char 的相互转换
2020-09-21 15:35:28#define uint32 unsigned int #define BREAK_UINT32( var, ByteNum ) \ (uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF)) #define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \ ...#include<stdio.h>
#define uint8 unsigned char
#define uint32 unsigned int#define BREAK_UINT32( var, ByteNum ) \
(uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF))#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
((uint32)((uint32)((Byte0) & 0x00FF) \
+ ((uint32)((Byte1) & 0x00FF) << 8) \
+ ((uint32)((Byte2) & 0x00FF) << 16) \
+ ((uint32)((Byte3) & 0x00FF) << 24)))#define BUILD_UINT16(loByte, hiByte) \
((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)#define BUILD_UINT8(hiByte, loByte) \
((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))#define HI_UINT8(a) (((a) >> 4) & 0x0F)
#define LO_UINT8(a) ((a) & 0x0F)int main(){
uint8 temp[4] = {0xF1,0xF2,0xF3,0xF4};
uint32 tevalu = 0;
tevalu = BUILD_UINT32(temp[0],temp[1],temp[2],temp[3]);
printf("---%08x-------%u\n",tevalu,tevalu);
uint8 temp2[4] = {0x0};
temp2[0] = BREAK_UINT32(tevalu,0);
temp2[1] = BREAK_UINT32(tevalu,1);
temp2[2] = BREAK_UINT32(tevalu,2);
temp2[3] = BREAK_UINT32(tevalu,3);
printf("--%02x--%02x--%02x--%02x--\n",temp2[0],temp2[1],temp2[2],temp2[3]);
unsigned int a = BUILD_UINT32(temp2[0],temp2[1],temp2[2],temp2[3]);printf("the unsigned int value is %u\n",a);
return 0;
}
-
IP地址转换为unsigned int,unsigned int 转换成IP地址
2020-09-15 09:28:21ip地址转换为unsigned int 方法: 例如ip地址“192.168.0.112”,四部分分别转换成整型的数t[4](利用atoi函数),转换后的数即为t[0]*... unsigned int uint_ip; }; 将整型数存入联合体中,利用char_ip[4]得出ip...ip地址转换为unsigned int 方法:
例如ip地址“192.168.0.112”,四部分分别转换成整型的数t[4](利用atoi函数),转换后的数即为t[0]*(256^3)+t[1]*(256^2)+t[2]*256+t[3];
unsigned int 转换成ip地址方法:
定义联合体
union IP { unsigned char char_ip[4]; unsigned int uint_ip; };
将整型数存入联合体中,利用char_ip[4]得出ip地址(利用itoa函数)
#include<iostream> using namespace std; union IP { unsigned char char_ip[4]; unsigned int uint_ip; }; unsigned int IptoUint(char * ip,int size) { unsigned int t[4]; int dot_ind[3]={0};//存储.位置 int j=0; for(int i=0;i<size;i++) { if(ip[i]=='.') { dot_ind[j++]=i; } } char *p=(char *)malloc(3); p=strncpy (p,ip,dot_ind[0]); t[0]=atoi(p); p=strncpy (p,ip+dot_ind[0]+1,dot_ind[1]-dot_ind[0]-1); t[1]=atoi(p); p=strncpy (p,ip+dot_ind[1]+1,dot_ind[2]-dot_ind[1]-1); t[2]=atoi(p); p=strncpy (p,ip+dot_ind[2]+1,size-dot_ind[2]-1); t[3]=atoi(p); unsigned int ip_uint=t[0]*256*256*256+t[1]*256*256+t[2]*256+t[3]; return ip_uint; } char* UinttoIp(const unsigned int a) { IP ip; ip.uint_ip=a; char * pp=new char[20]; memset(pp,0,20); for(int i=3;i>=0;i--)//由机器的大小端决定,此处是小端,即高位存在高地址,地位存在低地址 { char tmp[4]=""; itoa(ip.char_ip[i],tmp,10); strcat(pp,tmp); if(i>0) { char *dot="."; strcat(pp,dot); } } return pp; } int main() { char s[20]="";//"255.255.255.255"; cout<<"请输入ip地址:"; cin>>s; cout<<"您输入的ip地址是:"<<s<<endl; unsigned int b=IptoUint(s,strlen(s)); cout<<"转换成uint字符是:"<<b<<endl; cout<<"uint再转换为ip地址是:"<<UinttoIp(b)<<endl; return 0; }
-
【c】——unsigned int与unsigned short变量自动转换
2021-04-15 11:46:46c, 类型自动转换 code #include<iostream> ... printf("ushort:%d, uint:%d \n", sizeof(unsigned short), sizeof(unsigned int)); std::cout << a << b << std::end.c, 类型自动转换
code
#include<iostream> int main() { unsigned int a = 65537; unsigned short b; b = a; printf("ushort:%d, uint:%d \n", sizeof(unsigned short), sizeof(unsigned int)); std::cout << a << b << std::endl; return 0; }
输出
ushort:2, uint:4
655371解释:
- unsigned short占2个字节,16bit,unsigned int占4字节,32bit
- 65537的二进制为
10000000000000001
,转成unsigned short时,值保留低位的16bit,就是0000000000000001
,然后其转为整数就是1。
-
Question: TTL is signed int or unsigned int
2021-01-12 09:23:48<div><p>In the code, I see Ttl is uint32: type RR_Header struct { Name string <code>dns:"cdomain-name" Rrtype uint16 Class uint16 Ttl uint32 Rdlength uint16 // Length of data after header... -
uint8_t uint32_t 类型强制转换出错 以及 unsigned char 类型和 unsigned int 类型相互转化
2019-10-24 19:26:00typedef unsigned int uint32_t; uint8_t: u:代表 unsigned 即无符号,即定义的变量不能为负数; int:代表类型为 int 整形; 8:代表一个字节,即为 char 类型; _t:代表用 typedef 定义的; 整体代表:... -
int,Uint,uint16的区别及用处
2017-02-26 21:00:56int是C/C++数据类型,uint,uint16,uint32并不是C/C++内建的类型,而只是... //为了省事啊,这样不用写unsigned int而只需要写uint typedef unsigned short uint16;// int的size取决于平台,比如16位平台上sizeof -
uint8 int8
2019-07-27 08:46:35signed char int8; unsigned char uint8; int int16; unsigned int uint16; long int32; u... -
HLSL: Adding cast support for unsigned int (uint) for Load method, also upd…
2020-11-30 07:58:46t accepting uint inputs, because no entry in the symbol tables. <p>Added entries in symbol tables and also updated tests to check that loads from UINT are supported.</p><p>该提问来源于开源项目:... -
uint64_t {aka long unsigned int}
2021-02-08 09:32:18/* 类型长度 */ ...int main(int argc, char *argv[]) { uint64_t a = 5; printf("a = %d\n", a); return EXIT_SUCCESS; } 使用 %d 来打印 uint64_t,编译时提示警告: liyongjun@Box20:~/project -
int和uint相加减
2020-03-13 10:55:24#include <iostream> #include <stdio.h> using namespace std; int main() { int a = -20; unsigned int n; unsigned int b = 6; //用一个int去接,返回int if(((... -
-- char , unsigned char = BYTE, short, unsigned short = WORD, long, unsigned long = DWORD, int ,...
2011-02-16 11:25:00Turbo C有以 下几种类型: 整型(int)、浮点型(float)、字符型(char)、指针型(*)、无值型 (void)以及结构(struct)和联合(union)。其中前五种是Turbo C的基本数据类型、 后两种数据类型(结构和... -
Use uint64_t instead of 'unsigned long long int'.
2020-12-30 04:43:59<div><p>I wanted to do #9550, but before I get there, I realized that there is a worthwhile cleanup: We have historically used <code>unsigned long long int</code> for <code>global_dof_index</code> in ... -
Compiling ponyc fails with an "conversion to ‘size_t {aka unsigned int}’ from ‘uint64_t {aka long...
2021-01-11 08:53:00src/libponyc/reach/reach.c:34:10: error: conversion to ‘size_t {aka unsigned int}’ from ‘uint64_t {aka long long unsigned int}’ may alter its value [-Werror=conversion] return ponyint_hash_... -
Agent type system relies on the fact that uint32_t is identical to unsigned int
2020-12-25 19:39:46<div><p>Agent type system relies on the fact that uint32_t is identical to unsigned int. On the system I'm trying to compile it for they are of similar size. The compiler doesn't like it ... -
support unsigned uint64 for ultrajson
2021-01-08 05:45:24<p>I add a new type JT_ULONG to represent unsigned uint64 to the enums JSTYPES and add a function pointer getULongValue to struct _JSONObjectEncoder <p>when encoding a Pytheon Number to a C number in... -
uint8_t\uint16_t\uint32_t和unsigned char\int\long int的区别
2012-05-25 16:34:38本人刚步入嵌入式开发,看到ARM程序中,在定义数据类型时,有些用uint8_t神马的,有些用unsigned char什么的。 按说这两种类型是指同一种基本数据类型,在程序中这样用有什么特别用途吗??? -
Unsigned int Images
2020-12-09 02:31:56<div><p>On branch #654, I fixed a number of problems that we had related to reading in unsigned int Images. We don't have any unsigned int instantiations for the Image class in C++, so it ... -
unsigned char类型和unsigned int类型相互转化
2019-03-22 16:03:43写单片机程序的时候经常遇到unsigned char类型和unsigned int类型相互转化 下面写一个简单的例子实现互相转化的过程,比较简单,直接上代码...#define uint32 unsigned int #define BREAK_UINT32( var, ByteNum ) ... -
cannot convert 'volatile uint32_t* {aka volatile unsigned int*}' to 'volatile uint8_t*
2020-12-09 00:12:14volatile uint32_t* {aka volatile unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment u8g_data_port[0] = portOutputRegister(digitalPinToPort(ucg->pin_... -
uint与int区别
2020-09-29 10:12:37pecific integral type ... SpecifierCommon EquivalentSigningBitsBytesMinimum ValueMaximum Value int8_t signed char Signed 8 1 −128 127 uint8_t unsigned char Unsigned 8 1 0 255 int16_t short Signed 1... -
modified utype2[] to be corrected to use a fixed-size uint32 instead of unsigned int
2021-01-09 06:19:37<div><p>该提问来源于开源项目:fontforge/fontforge</p></div> -
int强转为uint64
2020-06-09 16:36:04int n ; uint64_t nRes = n; 当n>0时,nRes == n 但当n<0时,发现nRes会变成一个很大的数 带符号的类型强转成unsigned类型,高位会自动补符号位的字符 eg: n十六进制是80000001时,nRes十六进制会是... -
use uint64_t instead of unsigned __int64 in structs.h
2021-01-07 15:09:59<div><p>Updates #449. <p><strong>Note</strong>: this PR is not yet ready for merge. I just want to see where the build fails on Windows using the CI to build.</p><p>该提问来源于开源项目:... -
C++中UINT32和INT32以及int,BOOL和bool的区别
2015-07-26 10:41:02在AndroidHAL层开发中,编写C++代码的时候,遇到了数据类型的困扰,经过查找资料,总结如下: 1.UNIT32和int以及INT32的区别: (1).int默认是signed int。...typedef unsigned int UINT32 unsigned in