如何将单独的int值转换为十六进制字节数组 [英] How do I convert separate int values to hex byte array

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

问题描述

我需要做一些(对我而言)int / hex / byte的工作,并且我正在努力做到这一点。另一方面,tcp服务器期待Little Endian。

我需要发送一个由HEX值组成的字节数组。

6000 需要发送为:


0x70,0x17


19 需要将被发送为:


0x13,0x00,0x00,0x00




结果字节数组应该看起来像

  **来自制造商** 
完整信息应该是:

0x70,0x17,0x13,0x00,0x00,0x00,0x40,0x42,0x0f,0x00,0xA0,0x86,0x01,0x00,0x04,0x01,0x02,0x03,0x04
/ pre>

我可以将 6000 的十六进制值作为 1770 通过使用: .ToString(x4)
我可以通过使用 .ToString(x8)来获取 19 的十六进制值作为 00000013



我有两个问题:



  1. 如何获得





7017


的字节数组中,

[0] = 0x70,[1] = 0x17


您可以使用 us / library / system.bitconverter(v = vs.110).aspxrel =nofollow noreferrer> BitConverter 类来实现转换。
结果实际上已经在您需要的惯例中。不需要反转


  byte [] res6000 = BitConverter.GetBytes(6000); 
byte [] res19 = BitConverter.GetBytes(19);
$ b $ // TEST OUTPUT例如
Console.WriteLine(6000 - >:+ String.Join(,res6000.Select(x => x.ToString( X2\" ))));
Console.WriteLine(19 - >:+ String.Join(,res19.Select(x => x.ToString(X2))));

输出:


6000 - >:70170000

19 - >:13000000

这是一个小方法,这个作业用你想要的字节数:

  public byte [] TransformBytes(int num,int byteLength)
{
byte [] res = new byte [byteLength];

byte [] temp = BitConverter.GetBytes(num);

Array.Copy(temp,res,byteLength);

return res;
}

然后您可以调用它并将结果组合到列表中:

  List< byte> allBytesList = new List< byte>(); 

allBytesList.AddRange(TransformBytes(6000,2));
allBytesList.AddRange(TransformBytes(19,4));
allBytesList.AddRange(TransformBytes(1000000,4));
allBytesList.AddRange(TransformBytes(100000,4));
allBytesList.AddRange(TransformBytes(4,1));

Console.WriteLine(All - >:+ String.Join(,allBytesList.Select(x => x.ToString(X2))));

输出:


全部 - >:70 17 13 00 00 00 40 42 0F 00 A0 86 01 00 04



  byte [ ] b_array = allBytesList.ToArray(); 


I need to do some (new to me) int/hex/byte work and I am struggling to get it right. The tcp server on the other side is expecting Little Endian.

I need to send a byte array consisting of HEX values.

6000 needs to be sent as:

0x70, 0x17

19 needs to be sent as:

0x13, 0x00, 0x00, 0x00

The resulting byte array should look like this.

**FROM THE MANUFACTURER**
Complete message should be: 

0x70, 0x17, 0x13, 0x00, 0x00, 0x00, 0x40, 0x42, 0x0f, 0x00, 0xA0, 0x86, 0x01, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04

I can get the hex value of 6000 as 1770 by using: .ToString("x4") I can get the hex value of 19 as 00000013 by using: .ToString("x8")

I have two questions:

  1. This (to my knowledge) is Big Endian. Short from chopping the string and manually rewriting it to reverse it, is there a .net routine that can do this for me?

  2. Once I have it reversed, how do I get

7017

in a byte array of:

[0] = 0x70, [1] = 0x17

Thanks in advance.

解决方案

You can use the BitConverter class to achieve the conversion. The result is actually already in the convention that you need. No Reversion is necessary

byte[] res6000 = BitConverter.GetBytes(6000);
byte[] res19 = BitConverter.GetBytes(19);

// TEST OUTPUT for example
Console.WriteLine(" 6000 -> : " + String.Join("", res6000.Select(x => x.ToString("X2"))));
Console.WriteLine("  19  -> : " + String.Join("", res19.Select(x=>x.ToString("X2"))));

Output:

6000 -> : 70170000
19 -> : 13000000

Here is a little method that does the job, with the amount of bytes that you desire:

public byte[] TransformBytes(int num, int byteLength)
{
    byte[] res = new byte[byteLength];

    byte[] temp = BitConverter.GetBytes(num);

    Array.Copy(temp, res, byteLength);

    return res;
}

Then you could call it and combine the result in a list like this:

List<byte> allBytesList = new List<byte>();

allBytesList.AddRange(TransformBytes(   6000, 2));
allBytesList.AddRange(TransformBytes(     19, 4));
allBytesList.AddRange(TransformBytes(1000000, 4));
allBytesList.AddRange(TransformBytes( 100000, 4));
allBytesList.AddRange(TransformBytes(      4, 1));

Console.WriteLine(" All -> : " + String.Join(" ", allBytesList.Select(x => x.ToString("X2"))));

Output:

All -> : 70 17 13 00 00 00 40 42 0F 00 A0 86 01 00 04

The List<byte> can be easily converted in the end to an array:

byte [] b_array = allBytesList.ToArray();

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

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