c#byte*怎么转byte

 我来答
野人无事不言L
2016-10-14 · TA获得超过1413万个赞
知道顶级答主
回答量:2242万
采纳率:0%
帮助的人:144.3亿
展开全部
using System;
using System.Collections.Generic;
using System.Text;

namespace NatureSex.CommonTool.Basic
{
///<summary>
/// 位 操作工具类(也可以使用MS中System.Collections.BitArray操作)
/// author:NatureSex
///</summary>
publicsealedclass BitAssist
{
private BitAssist() { }

#region 获取

///<summary>
/// 取byteSource目标位的值
///</summary>
///<param name="byteSource">源字节</param>
///<param name="location">位置(0-7)</param>
///<returns>目标值</returns>
publicstaticint GetTargetBit(short location, byte byteSource)
{
Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
return GetTargetBit(location, byteSource, baseNum);
}

///<summary>
/// 取byteSource目标位的值
///</summary>
///<param name="location"></param>
///<param name="byteSource"></param>
///<param name="baseNum">与 基数(1,2,4,8,16,32,64,128)</param>
///<returns></returns>
privatestaticint GetTargetBit(short location, byte byteSource, byte baseNum)
{
if (location >7|| location <0) return-1000;
return (byteSource & baseNum) == baseNum ?1 : 0;
}

///<summary>
/// 取一批byteSources目标位的对应的值集合
///</summary>
///<param name="location">位置(0-7)</param>
///<param name="byteSources">一批字节</param>
///<returns>一一对应的目标值</returns>
publicstaticint[] GetTargetBit(short location, paramsbyte[] byteSources)
{
if (byteSources ==null) returnnull;
int bsLen = byteSources.Length;
Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
int[] result =newint[bsLen];
for (int i =0; i < bsLen; i++)
{
result[i] = GetTargetBit(location, byteSources[i], baseNum);
}
return result;
}

#endregion

#region 替换

///<summary>
/// 替换byteSource目标位的值
///</summary>
///<param name="location">替换位置(0-7)</param>
///<param name="value">替换的值(1-true,0-false)</param>
///<param name="byteSource">源字节</param>
///<returns>替换后的值</returns>
publicstaticbyte ReplaceTargetBit(short location, bool value, byte byteSource)
{
Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
return ReplaceTargetBit(location, value, byteSource, baseNum);
}

///<summary>
/// 替换byteSource目标位的值
///</summary>
///<param name="location"></param>
///<param name="value">替换的值(1-true,0-false)</param>
///<param name="byteSource"></param>
///<param name="baseNum">与 基数(1,2,4,8,16,32,64,128)</param>
///<returns></returns>
privatestaticbyte ReplaceTargetBit(short location,bool value,byte byteSource, byte baseNum)
{
if (location >7|| location <0)
{
thrownew FormatException("location params error!type range(0-7)");
}
bool locationValue=GetTargetBit(location, byteSource)==1?true:false;
if (locationValue != value)
{
return (byte)(value ? byteSource + baseNum : byteSource - baseNum);
}
return byteSource;
}

///<summary>
/// 替换一批byteSources目标位的对应的值集合
///</summary>
///<param name="location">替换位置(0-7)</param>
///<param name="value">替换的值(1-true,0-false)</param>
///<param name="byteSources">一批字节</param>
///<returns>替换后一一对应的值</returns>
publicstaticbyte[] ReplaceTargetBit(short location, bool value, paramsbyte[] byteSources)
{
if (byteSources ==null) returnnull;
int bsLen = byteSources.Length;
Byte baseNum = (byte)(Math.Pow(2, location +1) /2);
byte[] result =newbyte[bsLen];
for (int i =0; i < bsLen; i++)
{
result[i] = ReplaceTargetBit(location, value, byteSources[i], baseNum);
}
return result;
}

#endregion
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式