将扩展char转换为int java

时间:2017-05-17 14:27:49

标签: java string char int

我正在开发一款Android应用程序,它通过BLE接收String格式的数据。 该字符串包含HEX格式的ADC值。为了节省存储空间,我每个字节包含两个十六进制值(例如0xED)。但是,我无法从字符串中检索这些值。 所以我需要能够从字符串中获取int[],将每个索引的char分成两部分。

至于现在,我尝试获取charAt(index),然后将char投射到int。但是,这仅适用于ASCII有效数据。一旦一个字符的数据超过127,它就会返回异常的默认值。有什么方法可以避免这种情况吗?

以下代码适用于127以下的每个值,所以我只需要一个127以上的值的解决方案。



for (int i = 0; i < 119; i++) {
            char charTemp1 = datastring.charAt(i);
            int test = charTemp1 & 0xF0;
            int test2 = charTemp1 & 0xF;
            }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可能需要详细了解输入字符串格式的工作原理。您显示119个字符的字符串,据说包含十六进制数字对,我不明白。请注意,我在代码中给出了一个示例 holder.checkBox.setTag(R.integer.btnplusview, convertView); holder.checkBox.setTag( position); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview); TextView tv = (TextView) tempview.findViewById(R.id.animal); Integer pos = (Integer) holder.checkBox.getTag(); Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show(); if(modelArrayList.get(pos).getSelected()){ modelArrayList.get(pos).setSelected(false); }else { modelArrayList.get(pos).setSelected(true); } } }); 。你的字符串有何不同?

public class CustomAdapter  extends BaseAdapter {

    private Context context;
    public static ArrayList<Model> modelArrayList;


    public CustomAdapter(Context context, ArrayList<Model> modelArrayList) {

        this.context = context;
        this.modelArrayList = modelArrayList;

    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {

        return position;
    }

    @Override
    public int getCount() {
        return modelArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return modelArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lv_item, null, true);

            holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb);
            holder.tvAnimal = (TextView) convertView.findViewById(R.id.animal);

            convertView.setTag(holder);
        }else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder)convertView.getTag();
        }


        holder.checkBox.setText("Checkbox "+position);
        holder.tvAnimal.setText(modelArrayList.get(position).getAnimal());

        holder.checkBox.setChecked(modelArrayList.get(position).getSelected());

        holder.checkBox.setTag(R.integer.btnplusview, convertView);
        holder.checkBox.setTag( position);
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview);
                TextView tv = (TextView) tempview.findViewById(R.id.animal); 
                Integer pos = (Integer)  holder.checkBox.getTag();
                Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show();

                if(modelArrayList.get(pos).getSelected()){
                    modelArrayList.get(pos).setSelected(false);
                }else {
                    modelArrayList.get(pos).setSelected(true);
                }

            }
        });

        return convertView;
    }

    private class ViewHolder {

        protected CheckBox checkBox;
        private TextView tvAnimal;

    }

}