Keil 中如何从串口输出 unsigned long 型(4Byte)数据?
可以先把64位(4字节)的unsigned long转化成unsigned int;例如:unsigned long ul64;unsigned int uih,uil;//分别是64位的高32位和低32位;uih=ul64/32768;uil=ul64%32768;然后,把这两个32位数分别发送出去。
单字节、双字节、4字节都是什么意思
单字节指只占一个字,是英文字符。
双字是占两个字节的,中文字符都占两个字节 计算机中的数据都是以0和1来表示的,其中一个0或者一个1称之为一位,8位称为一个字节(Byte),两个字节称为一个字(Word)(双字节),4个字节称为双字(Dword)(四字节)。
单字节的英文是Byte;比特是最小的数值单位,它的英文是Bit。
一个字节包含8个比特,通常计算机初学者接触到的ASCII码就是单字节字符,由于计算机是二进制的,1个比特位只可以表示为0或者1这两种数,那8个比特(一般从右至左)就可以有0~127(2的8次方再减1)的不同的数值表示。
双字节字符即是包含了两个字节共16比特。
一般比较好理解的就是:英文字母属于单字节字符,而汉字则属于双字节字符。
因为英文字母、数字、符号等完全可以用128种不同的数值来表示,而汉字太多则不能,所以才需要扩展到双字节。
...
Java byte[4] z转换为int
byte[]数组转换成int类型的数是吧!public static int byteArrayToInt(byte[] b){ byte[] a = new byte[4]; int i = a.length - 1,j = b.length - 1; for (; i >= 0 ; i--,j--) {//从b的尾部(即int值的低位)开始copy数据 if(j >= 0) a[i] = b[j]; else a[i] = 0;//如果b.length不足4,则将高位补0 } int v0 = (a[0] & 0xff) int v1 = (a[1] & 0xff) int v2 = (a[2] & 0xff) int v3 = (a[3] & 0xff) ; return v0 + v1 + v2 + v3; }
汇编编程:用16位加法指令编一个32位(4字节)数加法程序。
就是高位的2个字节相加,低位的2个字节相加。
说个例子:a,b,是2个32位的数,相加后把值存到a中mov ax, word ptr a ;把a强制型转化成字mov dx, word ptr a+2add ax, word ptr badc dx, word ptr b+2mov word ptr a,axmov word ptr a+2,dx就ok啦!!主要就是ptr 的用法注意下哈!!...
用C++读取一个文件,文件前4byte是int型然后是char型等等,如何分类...
//设byte数组为 thisBytesbyte mask1 = 1; //声明一个屏蔽位: 00000001byte mask2 = 2; //声明第二个屏蔽位:00000010if ((mask1 & thisBytes[1]) == mask1){ //thisBytes[1]第0位是1thisBytes[2] |= 128; //10000000 //将thisBytes[2]最高位修改为1} else { //thisBytes[1]第0位是0thisBytes[2] &= 127; //01111111 //将thisBytes[2]最高位修改为0}同理利用mask2可修改thisBytes[3]最高位
如何实现函数strcpy,用C/C++/JAVA其中一种语言
char * strcpy (char *dest, const char *src) { return memcpy (dest, src, strlen (src) + 1); }memcpy 和 strlen 可以用高效(至少比 while(*s++)n++; 更高效)的方式实现 其实以上就是 glibc 2.22 中 strcpy 的实现。
不要以为这是简单的事,否则请看看 glibc 的 strlen 的实现:size_t strlen (const char *str) { const char *char_ptr; const unsigned long int *longword_ptr; unsigned long int longword, himagic, lomagic; /* Handle the first few characters by reading one character at a time. Do this until CHAR_PTR is aligned on a longword boundary. */ for (char_ptr = str; ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0; ++char_ptr) if (*char_ptr == '\0') return char_ptr - str; /* All these elucidatory comments refer to 4-byte longwords, but the theory applies equally well to 8-byte longwords. */ longword_ptr = (unsigned long int *) char_ptr; /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits the "holes." Note that there is a hole just to the left of each byte, with an extra at the end: bits: 01111110 11111110 11111110 11111111 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ himagic = 0x80808080L; lomagic = 0x01010101L; if (sizeof (longword) > 4) { /* 64-bit version of the magic. */ /* Do the shift in two steps to avoid a warning if long has 32 bits. */ himagic = ((himagic lomagic = ((lomagic } if (sizeof (longword) > 8) abort (); /* Instead of the traditional loop which tests each character, we will test a longword at a time. The tricky part is testing if *any of the four* bytes in the longword in question are zero. */ for (;;) { longword = *longword_ptr++; if (((longword - lomagic) & ~longword & himagic) != 0) { /* Which of the bytes was the zero? If none of them were, it was a misfire; continue the search. */ const char *cp = (const char *) (longword_ptr - 1); if (cp[0] == 0) return cp - str; if (cp[1] == 0) return cp - str + 1; if (cp[2] == 0) return cp - str + 2; if (cp[3] == 0) return cp - str + 3; if (sizeof (longword) > 4) { if (cp[4] == 0) return cp - str + 4; if (cp[5] == 0) return cp - str + 5; if (cp[6] == 0) return cp - str + 6; if (cp[7] == 0) return cp - str + 7; } } } }
转载请注明出处51数据库 » 4 byte words