如何将字节数组转换为32位整数数组 [英] How do I convert a byte array in to a 32bit integer array
问题描述
您好,
我正在编写一个应用程序,我需要将16位数作为产品代码返回给用户。我的一个对象返回一个字节数组长度128.我需要将这128个字节转换为16位。我正在尝试实现如下: -
Hi there,
I am writing an application where I need to return 16 digits to the user as product code.One of my objects returns a byte array of length 128. I need to convert these 128 bytes in to 16 digits.I am trying to achieve this as follows:-
Dim reg As New regProcess<br />
Dim ProductCode As Byte()<br />
Dim i As Integer = 0<br />
Dim j As Integer = 0<br />
Dim num As UInt64 = 0<br />
<br />
ProductCode = reg.Register<br />
If ProductCode.Length > 0 Then<br />
txt_ProductCode.Text = ""<br />
<br />
While i < (ProductCode.Length / 8)<br />
Dim var As UInt64 = 0<br />
For j = 0 To 7<br />
var += Convert.ToUInt64(ProductCode(i))<br />
<br />
i += 1<br />
Next<br />
txt_ProductCode.Text += Convert.ToString(var)<br />
<br />
End While<br />
End If<br />
End Sub
这并不能保证返回16位数,因为我需要连续8个字节的总和。
我的问题是,任何人都可以告诉我如何将这128个字节解释为32位整数,这样我就可以显示16位数字作为产品代码。
任何帮助都表示赞赏。
-Mrudula
This doesn't guarantee to return 16 digits as I am taking the sum of 8 consecutive bytes.
My question is can anybody tell me how do I interpret those 128 bytes as 32 bit integers, so that I can display 16 digits as product code.
Any help is appreciated.
-Mrudula
推荐答案
你不能。
128字节不适合成16位数。它将适合256个十六进制数字,但是将128个字节组合成16个数字的唯一方法是使用base 64作为输出值,这将是一个有趣的数字基数来使用。
你真的是指128位还是你在这里非常困惑?
You can't.
128 bytes will not fit into 16 digits. it will fit into 256 hexadecimal digits, but the only way to fit 128 bytes into 16 "digits" would be to use base 64 for your output values, which would be an "interesting" number base to use.
Do you really mean 128 bits or are you horribly confused here?
这篇关于如何将字节数组转换为32位整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!