c语言中的word和byte是什么数据类型啊
using System; class Program { static byte[] Str2ByteArr(string s) { int slen = s.Length; byte[] r = new byte[slen * 2]; int i = 0; foreach (char c in s) { r[i++] = (byte)(c >> 8); r[i++] = (byte)(c & 0xff); } return r; } static string ByteArr2Str(byte[] p) { if (p.Length % 2 != 0) throw new ArgumentException(); int slen = p.Length / 2; char[] rbuf = new char[slen]; int j = 0; for (int i = 0; i
c脚本怎样把字节数组转换成浮点数
/// 将二进制值转ASCII格式十六进制字符串/// /// 二进制值/// 定长度的二进制/// ASCII格式十六进制字符串 public static string toHexString(int data, int length) { string result = ""; if (data > 0) result = Convert.ToString(data, 16).ToUpper(); if (result.Length {// 位数不够补0 StringBuilder msg = new StringBuilder(0); msg.Length = 0; msg.Append(result); for (; msg.Length result = msg.ToString(); } return result; }////// 将浮点数转ASCII格式十六进制字符串(符合IEEE-754标准(32))/// /// 浮点数值/// 十六进制字符串 public static string FloatToIntString(float data) { byte[] intBuffer = BitConverter.GetBytes(data); StringBuilder stringBuffer = new StringBuilder(0); for (int i = 0; i { stringBuffer.Insert(0, toHexString(intBuffer[i] & 0xff, 2)); } return stringBuffer.ToString(); }/// /// 将ASCII格式十六进制字符串转浮点数(符合IEEE-754标准(32))/// /// 16进制字符串/// public static float StringToFloat(String data) { if (data.Length 8) {//throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4];// 将16进制串按字节逆序化(一个字节2个ASCII码) for (int i = 0; i { intBuffer[i] = Convert.ToByte(data.Substring((3 - i) * 2, 2), 16); } return BitConverter.ToSingle(intBuffer, 0); } }/// /// 将byte数组转为浮点数/// /// byte数组/// public static float ByteToFloat(byte[] bResponse) { if (bResponse.Length 4) {//throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4];//将byte数组的前后两个字节的高低位换过来 intBuffer[0] = bResponse[1]; intBuffer[1] = bResponse[0]; intBuffer[2] = bResponse[3]; intBuffer[3] = bResponse[2]; return BitConverter.ToSingle(intBuffer, 0); } }/// /// 用指针方式强制将byte数组转为浮点数/// /// /// public static float BytetoFloatByPoint(byte[] bResponse) {//uint nRest = ((uint)response[startByte]) * 256 + ((uint)response[startByte + 1]) + 65536 * ((uint)response[startByte + 2]) * 256 + ((uint)response[startByte + 3]); float fValue = 0f; uint nRest = ((uint)bResponse[0]) * 256+ ((uint)bResponse[1]) +65536 * (((uint)bResponse[2]) * 256 + ((uint)bResponse[3]));//用指针将整形强制转换成float unsafe { float* ptemp; ptemp = (float*)(&nRest); fValue = *ptemp; } return fValue; }
C语言中一个word是16个Bit、2个Byte,这个“word”是什么意思?
byte类型使用1字节存储,表示范围-128~127,int使用4字节存储,当byte+int时会将结果类型转为int,因为int表示范围大于byte,不影响精度,所以结果为197,但是当(byte)(a+b)时,是将计算结果int转为byte,存储空间也有4字节变为1字节,即是保留8个bit,把他们全部转换成2进制=11000110(198刚好8位),此时当前计算机保存这8个位,但是,计算机中表示数最高位会被识别为符号位,即0为正号,1位负号,不参与运算,因此当前2进制数被识别为负数,然而在计算机中负数使用补码方式进行表示,计算实际值需要将其转换成原码方式,转换方式:去除符号位剩余位按位取反(10111001),最低位+1(10111010),得出的值在计算大小(最高位依然表示正负),最终得出-58如果想要搞清楚,请在中查询"数据在机器层次的表示方法"来获取更详细的信息
转载请注明出处51数据库 » c byte转word