如何将字符串转换为字节数组? [英] How do I convert a string into array of byte?

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

问题描述

您好,

我想转换字节地址中的数据类型。





+我必须从我的数据中逃脱0x。

数据类型将是字符串(我认为)但它可能是int或其他。

我不知道在代码中写什么。

谢谢!

例如:



我尝试过:



public void blabla(string 0xmyaddress)

{

///////我的地址如下:0x01020304

///////我想将该地址转换为:

byte [0] = 04;

byte [1] = 03;

byte [2] = 02;

byte [3] = 01; //所有都是以字节的相反顺序,我是怎么做的。



//并且会有我的代码(我希望)



}

Hello,
I want to convert a data type in a byte address.


+ And i have to escape that "0x" from my data.
And the data type will be as string(i think) but it may be int or whatever.
I don't know what to write in code.
Thanks!
For example:

What I have tried:

public void blabla(string 0xmyaddress)
{
/////// My address will be like : 0x01020304
/////// And i want to convert that address to:
byte [0]= 04;
byte [1]= 03;
byte [2]= 02;
byte [3]= 01; // all in reverse order of bytes, how i did.

//AND there will be my code(i hope)

}

推荐答案

你认为这可能更复杂:一个字符串是一个字符数组(或者它可以被思考和处理因此,但.NET中的 char 不等同于一个字节,它是一个Unicode值,因此是一个16位值,不像字节是一个8位值。



有很多方法可以转换它,但它们依赖于知道字符串中的原始值是什么。如果你知道一个字符串包含例如ASCII数据 - 因为它是从一个工作在ASCII中的设备接收的 - 那么它很简单:

That's possibly more complicated that you think: a string is an array of characters (or it can be thought of, and treated as such) but a char in .NET is not equivalent to a byte, it is a Unicode value and is thus a 16 bit value, unlike byte which is an 8 bit value.

There are ways to convert it, but they depend on "knowing" what the original value in the string were intended to be. If you know that a string contains ASCII data for example - because it's been received from a device that works in ASCII - then it's simple:
string s = ...
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);



但如果你不知道消息来源,那么你可能不知道内容并且转换很容易出错。



下一个问题是你的例子没有多大意义:它似乎暗示了三件事之一:

1)你想转换名字一个字节数组的局部变量是愚蠢的,并且不太可靠:名称不一定是由编译保留的,可能是一组随机字母而不是文字Ox01020304

2您正在尝试将字符串的地址转换为字节数组。这也更复杂,因为字符串没有直接地址:它是对堆内存中不变项的引用,如果需要透明地在幕后,GC可以自由地将实际对象移动到内存中。要使用地址本身,你必须首先修复它的位置,这不是通常需要的东西。

3)如果Oxmyaddress的内容是一个字符串0x01020304那么它是非常微不足道的:使用Convert.ToInt32将其转换为整数,然后使用BitConverter获取字节:


But if you don't know the source, then you probably don;t know the content and the conversion is prone to errors.

The next problem is that your example doesn't make a lot of sense: it seend to imply one of three things:
1) You want to convert the name of a local variable to an array of bytes which is silly, and unlikely to be reliable: the name is not necessarily preserved by compilation and may be a random set of letters instead of a literal "Ox01020304"
2) You are trying to convert the "address" of the string to an array of bytes. That's more complicated as well, because the string doesn't have an address directly: it's a reference to an invariant item in heap memory, and the GC is at liberty to move the actual object around in memory if it needs to transparently behind the scenes. To use the address itself, you have to "fix" it's position first, and that isn't something normally needed.
3) If the content of Oxmyaddress is a string "0x01020304" then it's pretty trivial: use Convert.ToInt32 to convert it to an integer, and then BitConverter to get the bytes:

string s = "0x01020304";
int i = Convert.ToInt32(s, 16);
byte[] buffer = BitConverter.GetBytes(i);





但我认为你需要先仔细考虑一下你在做什么......



But I think you need to consider exactly what you are doing a bit more carefully first...


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

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