unsigned

How to convert an NSString to an unsigned int in Cocoa?

六眼飞鱼酱① 提交于 2020-01-01 05:08:50
问题 My application gets handed an NSString containing an unsigned int . NSString doesn't have an [myString unsignedIntegerValue]; method. I'd like to be able to take the value out of the string without mangling it, and then place it inside an NSNumber . I'm trying to do it like so: NSString *myUnsignedIntString = [self someMethodReturningAString]; NSInteger myInteger = [myUnsignedIntString integerValue]; NSNumber *myNSNumber = [NSNumber numberWithInteger:myInteger]; // ...put |myNumber| in an

ping 实现设计---ICMP

江枫思渺然 提交于 2020-01-01 03:42:00
发送ICMP报文时,必须程序自己计算校验和,将它填入ICMP头部对应的域中。 校验和的计算方法:   将数据以字为单位累加到一个双字中,如果数据长度为奇数,最后一个字节将被扩展到字,累加的结果是一个双字,最后将这个双字的高16位,低16位相加后取反,便得到了校验和。 下面是checksum的计算校验和的代码: USHORT checksum(USHORT* buff, int size) { unsigned long cksum = 0; while(size>1) { cksum += *buff++; size -= sizeof(USHORT); } // 是奇数 if(size) { cksum += *(UCHAR*)buff; } // 将32位的chsum高16位和低16位相加,然后取反 cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >> 16); return (USHORT)(~cksum); } Ping程序实例: Ping用来检查主机是否存在,是否可达。 下面是Ping的执行步骤: 1 创建协议类型为IPPROTO_ICMP的原始套接字 2 创建并初始化ICMP封包 3 调用sendto函数向远程主机发送ICMP请求 4 调用recvfrom函数接收ICMP响应 完整代码如下: /////

数码管、按键、定时器、ds18b20综合应用

断了今生、忘了曾经 提交于 2020-01-01 00:53:01
利用 8051 单片机设计秒表,包括 3 个按键、3 位数码管。按键分别用于启动、暂停、 显示学号,显示温度,时间用数码管显示。开始后显示学号后 3 位。按启动键下后,显示 0.00 以 0.01 秒的频率从 0.00 递增,期间按暂停键时暂停,再次按暂停继续递增,当递增到 9.99 以后循 环。按显示学号键,显示学号。 1、电路仿真: 2、程序设计: (1)项目结构: (2)主函数: #include <reg52.h> #define uchar unsigned char #define uint unsigned int #include<display.c> #include<key.c> #include<DS18B20.C> unsigned int cp; void display(void); void key(void); /*Timer0中断服务函数*/ void timer0_isr(void) interrupt 1 { TH0=(65535-2000)/256; //重装初值 TL0=(65535-2000)%256; //重装初值 cp++; //2000代表2ms,1000ms=1s if(mode == 2)cp=0;//按下暂停按键后,mode为2 else{ if(cp>=5) //0.01s到了 { cp=0; xiaoshu++; } if

C++简单实现Base64的编解码

不问归期 提交于 2019-12-31 17:54:58
Base64是个很简单的将数据编码为可见字符的算法。就是将原始数据每6个位取出来,找一个可见字符代替,这样4个可见字符刚好可以代替原来的3个字节,原理不多说,直接上代码。 PS:代码只实现对可见字符的加解密,不可打印的还要修改。 #include <string> using namespace std; string Code("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); string Base64Encode(string data) { size_t len = data.size() / 3; string out; const unsigned char* input = (unsigned char*)data.data(); for (size_t i = 0; i < len; i++){ out += Code[input[0] >> 2]; out += Code[((input[0] & 0x03) << 4 )+ (input[1] >> 4)]; out += Code[((input[1] & 0x0f) << 2) + (input[2] >> 6)]; out += Code[input[2] & 0x3f]; input += 3; } len = data

C# Unsigned bytes Encryption to Java Signed bytes Decryption

纵然是瞬间 提交于 2019-12-31 03:17:26
问题 I have an application in C# that encrypt part of my files (because they are big files) using RijndaelManaged. So I convert my file to byte arrays and encrypt only a part of it. Then I want to decrypt the file using Java. So I have to decrypt only part of the file (means those bytes) that was encrypted in C#. Here the problem comes. Because in C# we have unsigned bytes and in Java we have signed bytes . So my encryption and decryption not working the way I want. In C# I have joined the

How to convert signed 32-bit int to unsigned 32-bit int?

北慕城南 提交于 2019-12-30 06:11:28
问题 This is what I have, currently. Is there any nicer way to do this? import struct def int32_to_uint32(i): return struct.unpack_from("I", struct.pack("i", i))[0] 回答1: Not sure if it's "nicer" or not... import ctypes def int32_to_uint32(i): return ctypes.c_uint32(i).value 回答2: using numpy for example: import numpy result = numpy.uint32( numpy.int32(myval) ) or even on arrays, arr = numpy.array(range(10)) result = numpy.uint32( numpy.int32(arr) ) 来源: https://stackoverflow.com/questions/16452232

Understanding Java unsigned numbers

北城以北 提交于 2019-12-30 04:58:25
问题 I want to understand how to convert signed number into unsigned. lets say I have this: byte number = 127; // '1111111' In order to make it unsigned I have to choose "bigger" data type 'short' and apply AND operator with value 0x00ff. short number2; number2 = number & 0x00ff; Why does it make the number unsigned? 回答1: Java doesn't actually have unsigned primitives. The value 127 is actually represented by '01111111' the first bit being the sign (0 is positive). An unsigned byte would be able

Compute the absolute difference between unsigned integers using SSE

我只是一个虾纸丫 提交于 2019-12-30 03:09:15
问题 In C is there a branch-less technique to compute the absolute difference between two unsigned ints? For example given the variables a and b, I would like the value 2 for cases when a=3, b=5 or b=3, a=5. Ideally I would also like to be able to vectorize the computation using the SSE registers. 回答1: There are several ways to do it, I'll just mention one: SSE4 Use PMINUD and PMAXUD to separate the larger value in register #1, and the smaller value in register #2. Subtract them. MMX/SSE2 Flip the

Why do MIPS operations on unsigned numbers give signed results?

﹥>﹥吖頭↗ 提交于 2019-12-30 01:55:12
问题 When I try working on unsigned integers in MIPS, the result of every operation I do remains signed (that is, the integers are all in 2's complement), even though every operation I perform is an unsigned one: addu , multu and so fourth... When I print numbers in the range [2^31, 2^32 - 1] I get their 'overflowed' negative value as if they were signed (I guess they are). Though, when I try something like this: li $v0, 1 li $a0, 2147483648 # or any bigger number syscall the printed number is

int long 等基础类型在不同平台的大小

天涯浪子 提交于 2019-12-30 00:03:44
转自 http://www.cnblogs.com/yishuiliunian/archive/2011/03/18/1988244.html 上次腾讯面试,问我int和long分别几个字节,结果被鄙视了。 事过N天,鼓起勇气来好好查了一下,发现学问还是蛮大的。 int类型比较特殊,具体的字节数同机器字长和编译器有关。如果要保证移植性,尽量用__int16 __int32 __int64吧,或者自己typedef int INT32一下。 C、C++标准中只规定了某种类型的最小字节数(防止溢出) 64位指的是cpu通用寄存器的数据宽度是64位的。找到一个图 数据类型名称 字节数 别名 取值范围 int * signed,signed int 由 操作系统 决定,即与操作系统的"字长"有关 unsigned int * unsigned 由操作系统决定,即与操作系统的"字长"有关 __int8 1 char,signed char –128 到 127 __int16 2 short,short int,signed short int –32,768 到 32,767 __int32 4 signed,signed int –2,147,483,648 到 2,147,483,647 __int64 8 无 –9,223,372,036,854,775,808 到 9,223