将整数转换为字节数组VB.net [英] Convert integers to a Byte array VB.net

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

问题描述

我有以下Java代码可以正常工作,在写入流之前将一些数字转换为字节数组。

I have the following Java code working as expected, to convert some numbers to an array of Bytes before writing to a stream.

byte[] var1 = new byte[]{
    (byte)-95,
    (byte)(240 / 256 / 256 % 256),
    (byte)(240 / 256 % 256),
    (byte)(240 % 256),
    (byte)0
};

我需要在VB .net
中写相同的代码我在VB中尝试了以下代码.net,但没有成功。

I need to write the same in VB .net I tried the following code in VB .net, but no success.

Dim var1(4) As Byte
    var1(0) = Byte.Parse(-95)
    var1(1) = Byte.Parse(240 / 256 / 256 Mod 256)
    var1(2) = Byte.Parse(240 / 256 Mod 256)
    var1(3) = Byte.Parse(240 Mod 256)
    var1(4) = Byte.Parse(0)

我做错了吗?

谢谢。

推荐答案

您可以使用(32位(4字节))转换为字节数组。 bitconverter%28v = vs.110%29.aspx rel = noreferrer> BitConverter 类。

You can convert an integer (32 bit (4 byte)) to a byte array using the BitConverter class.

Dim result As Byte() = BitConverter.GetBytes(-95I)

Dim b1 As Byte = result(0) '161
Dim b2 As Byte = result(1) '255
Dim b3 As Byte = result(2) '255
Dim b4 As Byte = result(3) '255

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

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