Arduino的.toCharArray()方法有bug吗?

时间:2016-06-25 04:28:56

标签: arrays string arduino

假设我有一个字符串" AB",我想转换为char []数组并且 将HEX中的两个char数组元素打印到串行监视器。应该很简单。但是,第二个元素始终打印为0。

AB
41 0

我得到的输出是

    $query = array();
    $query['cmd'] = '_xclick-subscriptions';
    $query['upload'] = '1';

    $query['business'] = $select_paypal_detail[0]->business_mail;

    $product = $_POST['package'];
    //$product_price = $_POST['price'];
    $product_quantity = $_POST['quantity'];

    $query['a3'] = $_POST['price'];
    $query['p3'] = 1;
    $query['t3'] = 'D';
    $query['no_note'] = 1;

    //$query['amount'] = $_POST['price'];

    $query['discount_rate'] = $_POST['discount'];
    $query['item_name'] = $_POST['package'];

    $query['quantity'] = $_POST['quantity'];

    $query['currency'] =  'CAD';
    $query['instId'] = '211616';
    $query['testMode'] = 100;
    $_SESSION['order_number'] = $order_number;
    $query['cartId'] = 'Order Number '.$_POST['order_number'];
    $query['return'] = $select_paypal_detail[0]->return_url;
    $query['notify_url'] = "http://theelitecoachingacademy.com/?AngellEYE_Paypal_Ipn_For_Wordpress&action=ipn_handler";

    // Prepare query string
    $query_string = http_build_query($query);   

    if( $select_paypal_detail[0]->payment_type == 'live' ){
    ?>

    <!--/header('Location: https://www.paypal.com/cgi-bin/webscr?' . $query_string);
    header('Location: https://www.sandbox.paypal.com/cgi-bin/webscr?' . $query_string);-->

    <script type="text/javascript">
                 window.location="https://www.paypal.com/cgi-bin/webscr?<? echo $query_string ?>";
    </script>
    <?php } else { ?>
    <script type="text/javascript">
                 window.location="https://www.sandbox.paypal.com/cgi-bin/webscr?<? echo $query_string ?>";
    </script>
    <?php
}?>

1 个答案:

答案 0 :(得分:1)

toCharArray会将尽可能多的字符复制到缓冲区中,因为它仍然可以 返回有效的C字符串 。有效的C字符串具有nul(00)终止字节。如果你给它一个2字节的缓冲区,它只能在nul终结符之前适合一个字符。

(查看Arduino源,它会计算要复制的字符数。https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/WString.cpp#L544 bufsize - 1是为空终止符留出空间的地方。)

你的代码应该是 char myarray[3]; // Make room for 2 chars + nul terminator line.toCharArray(myarray, sizeof(myarray)); // Use sizeof to avoid repeating '2'

但实际上,根本不需要缓冲区来复制。 String已经有一个字符访问器charAt() Serial.print(line.charAt(0), HEX); Serial.print(line.charAt(1), HEX);

相关问题