将bytearray转换为类似字节的对象?

时间:2017-09-05 22:16:20

标签: arrays python-3.x

在Python 3中,我收到错误TypeError: a bytes-like object is required, not 'bytearray'

我有一个bytearray,看起来像这样:

 >>> print(my_ba)
 bytearray(b'}\x0e\x15/ow4|-')

如果我在控制台中输入它,它可以工作:

 >>> print(base58.b58encode(b'}\x0e\x15/ow4|-'))
 2bKmhuGiGP7t8

但这会产生错误,我无法找到如何获得b'' bytearray中的字符串:

 >>> print(base58.b58encode(my_ba)
 TypeError: a bytes-like object is required, not 'bytearray'

我确定这很明显,但如何将bytearray转换为带有b前缀的字符串?

1 个答案:

答案 0 :(得分:10)

正如Coldspeed在评论中所说,只需将一个bytearray传递给bytes(my_ba)来电:

package org.terna.notes_aap; /** * Created by Sohail on 8/23/2017. */ import android.content.Context; import android.content.Intent; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.ViewGroup; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.chauthai.swipereveallayout.SwipeRevealLayout; import com.chauthai.swipereveallayout.ViewBinderHelper; import java.util.ArrayList; import java.util.List; import static android.R.attr.name; public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> { private List<Note> noteList; private Context context; private ItemClickListener clickListener; private LayoutInflater mInflater; int i; private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper(); public void setClickListener(ItemClickListener itemClickListener) { this.clickListener = itemClickListener; } public NotesAdapter(List<Note> noteList, Context context) { this.noteList = noteList; this.context = context; mInflater = LayoutInflater.from(context); viewBinderHelper.setOpenOnlyOne(true); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_row, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(final MyViewHolder holder, int position) { Note note = noteList.get(position); holder.tid.setText(""+note.getId()); holder.title.setText(note.getTitle()); holder.description.setText(note.getDescription()); holder.date.setText(note.getDate()); holder.month.setText(note.getMonth()); holder.year.setText(note.getYear()); if (noteList != null && 0 <= position && position < noteList.size()) { final String data = noteList.get(position).toString(); holder.bind(data); } } @Override public int getItemCount() { if (noteList == null) return 0; return noteList.size(); } public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private SwipeRevealLayout swipeLayout; private View deleteLayout; public TextView tid, title, description,month, date, year; // this relativelayout is my row in recyclerview were i want the on click event. public RelativeLayout relativeLayout; Button button1; PersonDB db1=new PersonDB(context,"persondb2.db",null,1); ArrayList<PersonModel> persons = db1.getAllPersons(); String id1; // i have created all objects here. public MyViewHolder(View view) { super(view); swipeLayout = (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout); deleteLayout = itemView.findViewById(R.id.delete_layout); tid = (TextView) view.findViewById(R.id.id); title = (TextView) view.findViewById(R.id.title); description = (TextView) view.findViewById(R.id.description); month = (TextView) view.findViewById(R.id.month); date = (TextView) view.findViewById(R.id.date); year = (TextView) view.findViewById(R.id.year); relativeLayout = (RelativeLayout) view.findViewById(R.id.list_row); button1 =(Button)view.findViewById(R.id.button1); relativeLayout.setOnClickListener(this); } // this is for deletion of items in recycler view, its working fine. public void bind(String data) { button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i = Integer.parseInt(tid.getText().toString()); db1.deletePerson(i); Toast.makeText(context, "DELETED id = "+tid.getText(), Toast.LENGTH_SHORT).show(); noteList.remove(getAdapterPosition()); notifyItemRemoved(getAdapterPosition()); } }); } // I am passing the 'i' parameter because i want the id of the item which is clicked, so that i can perform update, delete note operation. // Is there any Other way to do it @Override public void onClick(View view) { i = Integer.parseInt(tid.getText().toString()); if (clickListener != null) clickListener.onClick(view, getAdapterPosition(),i); // call the onClick in the OnItemClickListener } } public int getdata(){ int s = i; return s; } }