C#将字符串转换为字节,输出为字符串

时间:2016-06-26 23:01:05

标签: c# string type-conversion byte

我想将public View getInfoContents(Marker marker) { View v = getActivity().getLayoutInflater().inflate(R.layout.maps_infowindow, null); v.setLayoutParams(new LinearLayout.LayoutParams((int) (mapFragment.getView().getMeasuredWidth() * .9), LinearLayout.LayoutParams.WRAP_CONTENT)); ((TextView) v.findViewById(R.id.title)).setText(marker.getTitle()); ((TextView) v.findViewById(R.id.desc)).setText(marker.getSnippet()); ImageView icon = (ImageView) v.findViewById(R.id.imageView5); //icon.setImageResource(R.mipmap.back_vision_fade); ArrayList<Item> list = new ArrayList<Item>(); ParseQuery<ParseObject> query = ParseQuery.getQuery("Places"); if(searchType!=null && searchType.length()>0) { ArrayList<String> types = new ArrayList<String>(); for(String type: searchType.split("\\|")) types.add(type); query.whereContainedIn("icon", types); } try { List<ParseObject> objects = query.find(); for(ParseObject obj : objects){ if(obj.getParseFile("icon")!=null) { Picasso.with(getActivity()).load(obj.getParseFile("icon").getUrl()).into(icon, new MarkerCallback(marker)); //item.downloadIcon(getActivity()); } } } catch (ParseException e) { } return v; } } ); 转换为string并将其输出为byte

例如: 字符串:string 输出:255

0xFF

我得到richTextBox1.AppendText(textBox1.Text + " || " + Convert.ToBytes(textBox1.Text) + "\n"); 而不是价值。

1 个答案:

答案 0 :(得分:0)

数组没有实现这一点。你必须自己写。类似下面的代码:

byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6 };
            string output = string.Empty;
            foreach (byte item in bytes)
            {
                output += Convert.ToString(item, 16).ToUpper().PadLeft(2,'0');

            }
            Console.WriteLine(output);
            //or using string.Format
            bytes = new byte[] { 1, 2, 3, 14, 15, 16 };
             output = string.Empty;
            foreach (byte item in bytes)
            {
                output = string.Format("{0},{1:X}", output, item);

            }
            Console.WriteLine(output);