转换一个字符串数组到字节数组 [英] Convert a string array to byte array

查看:148
本文介绍了转换一个字符串数组到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个文件,其内容为字符串数组但最初我有只字节数组,所以首先我想它转换成字符串数组,所以我怎么可以这样做。

I want to make a file which reads String array but initially I am having only byte array so first I want to convert it into string array, so how can I do so.

推荐答案

试试这个:

Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(yourString);

您可能需要根据的character编码您的字符串 - 请的 System.Text.Encoding (特别是它的属性)对于由这种类型支持其他编码。

You may need to change this up depending on the character encoding of your string - please see System.Text.Encoding (specifically its properties) for other encodings that are supported by this type.

如果你需要走另一条路(和转换字节[] 字符串),那么做到这一点(对字符编码的通知仍然在这里也适用):

If you need to go the other way (and convert a Byte[] to a String) then do this (The advice on character encoding still applies here as well):

String yourString = System.Text.Encoding.UTF8.GetString(yourByteArray);

这听起来像你正在使用的API需要一个的String [] 的GetString 将为您提供字符串,而不是只是一个实例数组。或许,这样的事情会为你的API调用工作:

It sounds like your the API you are using expects a String[] and a call to GetString will provide you with just a single instance of String, not an array. Perhaps something like this will work for your API call:

String yourString = System.Text.Encoding.UTF8.GetString(yourByteArray);
someType.ApiCall(new[] { yourString });

这篇关于转换一个字符串数组到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆