如何使用C#从位信息创建char数组

时间:2018-01-24 12:50:47

标签: c# arrays bits

我有以下结构:

enter image description here

我需要创建一个包含信息值的char数组,我的意思是,我需要这样的东西:

~

最好的方法是什么?我做的是获取字节,然后传递给位数组,接下来创建一个包含信息位置的字符串,最后在字符串上应用ToCharArray()方法并提供所需信息,但我想知道是否存在最佳方法

<?php
global $con;

if (isset($_POST['btnSubmit']))
{
$id = $_POST['id'];
$date = $_POST['date'];
$SalesPerson = $_POST['SalesPerson'];
$Telephone = $_POST['Telephone'];
$CustomerName = $_POST['CustomerName'];


$socupay = $_POST['socupay'];
$sobalance = $_POST['sobalance'];
$itemList = $_POST['demo'];
$namet=$_POST['item_name'];
$Quantity = $_POST['Quantity'];
$i=0;
$j=1;


foreach($namet as $name1)
{

$quantity=$_POST['Quantity'][$i];

$count = "SELECT count(*) FROM bincard WHERE ItemName='$name1' and QtyIssued >=$Quantity";
if($count == 1)
{

$amount = "SELECT QtyIssued FROM bincard WHERE ItemName='$name1'";
$amount1 = $amount - $Quantity;

$db->execute("UPDATE bincard SET QtyIssued=$amount1 WHERE ItemName='$name1'");
$j++;
}
else
{
echo "<br><font color=green size=+1 >There is no enough stock deliver for $name1! Please add stock !</font>" ;
}
$i++;
}


if( $socupay == NULL && $itemList == NULL)
{
echo"<script> alert('Please fill all the fields')</script>";
}
else
{           
$sql = "insert into invoiceorder (InvoId,Date,SalesPerson,Telephone,Customer,CurrentPayment,Balance) 
values ('$id','$date','$SalesPerson','$Telephone','$CustomerName','$socupay','$sobalance')";

$sql1 = $itemList;

$query = mysqli_query($con, $sql);

$query1 = mysqli_query($con, $sql1);


if($query && $query1)
{
echo "<script>alert('Press ok to print!')</script>";
echo "<script>window.open('invoiceprint.php?id=$id','_self')</script>";
}   
else
{
echo "Error: " . $sql . "<br>" . $con->error;
}       
}

}
?>

提前致谢。

1 个答案:

答案 0 :(得分:2)

大概是这样的:

char[] arr = new char[4];
arr[0] = (bytes[2] & 0x01) != 0 ? '1' : '0';
arr[1] = (bytes[2] & 0x02) != 0 ? '1' : '0';
arr[2] = (bytes[3] & 0x01) != 0 ? '1' : '0';
arr[3] = (bytes[3] & 0x02) != 0 ? '1' : '0';

请注意,有一些方法可以更简单地完成此操作(例如,将两个位的所有4种可能组合作为字符串,并且只进行查找) - 但这在一般情况下有效。