字节字符串到字节数组。并发症!见说明 [英] Byte String to Byte Array. Complications!! See Description

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

问题描述

场景是我需要从文件中加载字节串并将它们转换为字节数组并进行处理。问题是当我将字节字符串转换为字节时,值字节数组已保存已转换为十进制,我不想要

Scenario is i need to load byte stings from the file and convert them into byte array and process it. Problem is when i convert the byte string into bytes the values Byte array holds already converted into decimal, which I don't want

string HexMessage = "00790b1a"



以字节为单位


Equivalent in Bytes

byte[] HexBytes = { 0x00, 0x79, 0x0b, 0x1a }



当我使用指定的函数转换它时


OR when i convert it using the specified funtion

byte[] HexBytes = StringToByteArray(HexMessage);



但是当你访问它转换成十进制的元素并存储在像



HexBytes [0] = 0;

HexBytes [2] = 121;

HexBytes [3] = 11;

HexBytes [4] = 26;



我需要将原始字节存储在数组中,这样当我想转换2个字节时我可以得到正确的shortint值。



HexBytes [0] = 0;

HexBytes [2] = 79;

HexBytes [3] = 0b;

HexBytes [4] = 1a;





功能IM使用是


But when u access the elements its converted into decimal and stored in byte arrays like

HexBytes[0] = 0;
HexBytes[2] = 121;
HexBytes[3] = 11;
HexBytes[4] = 26;

I need the original bytes to be stored in the array so when i want to convert 2 bytes i could get correct value for shortint.

HexBytes[0] = 0;
HexBytes[2] = 79;
HexBytes[3] = 0b;
HexBytes[4] = 1a;


FUNCTION IM USING IS

public static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
        }







我一直在敲我的脑袋2天无法找到解决办法它。如果有人已经弄明白这一点,请告诉我。谢谢提前




I have been banging my head for 2 days couldnt find the solution to it. Let me know if anyone has already figured this out. Thanks In advance

推荐答案

像谢尔盖和托马斯在评论中提到的没有什么不错



dec / hex / bin

121 = 79 = 0111 1001

26 = 1A = 0001 1010



值已经是你想要的了。

它只显示你想要的十进制数(或者更好的想法应该是)hex



如果你不想显示你的值为Hex String只需使用 .ToString(X)

但不要将该字符串用于数学或其他东西!仅用于显示目的,它仅代表您已有的数据



121(dec)的比特是:0111 1001

存储为Hex-String(79)位是:0011 0111 0011 1001

完全不同的东西!
like Sergey and Tomas mentioned in the Comments there is nothing wrong

dec / hex / bin
121 = 79 = 0111 1001
26 = 1A = 0001 1010

the value in the array is already what you want.
It's only displayed in decimal where you wanted (or better thought it should be) hex

if you wont to display your value as Hex String just use .ToString("X")
but don't use that String for math or other stuff! only for display purposes its only a representation of the Data you already have

Bits of 121(dec) are: 0111 1001
stored as "Hex-String"(79) bits are: 0011 0111 0011 1001
totally different thing!


Decimal 十六进制是一个数字的两个不同的表示

Decimal and hexadecimal are two different representations of a number.
HexBytes[3] = 11; //(1) this is fine




HexBytes[3] = b; //(2) such a statement is not valid




HexBytes[3] = 0xb; //(3) this is fine and completely equivalent to (1)





请注意,(1)和(3)已翻译完全编译器的相同语句。



Please note that (1) and (3) are translated to exactly the same statement by the compiler.


这篇关于字节字符串到字节数组。并发症!见说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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