如何将字节数组转换为整数数组 [英] how to convert the byte array into an integer array

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

问题描述

我正在开发一个项目,我在其中接收图像数据作为字节数组(每像素1个字节)。每个字节代表一个灰度整数(0-255)。

I am working on a project in which I am receiving image data as an array of bytes (1-byte per pixel). Each byte represents a greyscale integer (0-255).

为了对数据执行许多功能,我需要将字节数组转换为整数数组。
请帮帮我..

In order to perform a number of functions on the data, I need to convert the byte array into an integer array. please help me..

推荐答案

简单方法有什么问题吗?

Anything wrong with the simple approach?

public static int[] convertToIntArray(byte[] input)
{
    int[] ret = new int[input.length];
    for (int i = 0; i < input.length; i++)
    {
        ret[i] = input[i] & 0xff; // Range 0 to 255, not -128 to 127
    }
    return ret;
}

编辑:如果你想要-128到127的范围:

If you want a range of -128 to 127:

public static int[] convertToIntArray(byte[] input)
{
    int[] ret = new int[input.length];
    for (int i = 0; i < input.length; i++)
    {
        ret[i] = input[i];
    }
    return ret;
}

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

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