How to convert int[] to byte[]

前端 未结 6 523
花落未央
花落未央 2020-11-27 04:39

I have an array of integers which represent a RGB image and would like to convert it to a byte array and save it to a file.

What\'s the best way to convert an array

6条回答
  •  醉话见心
    2020-11-27 05:01

    I created this code and it's working pretty well:

        int IntToByte(byte arrayDst[], int arrayOrg[], int maxOrg){
            int i;
            int idxDst;
            int maxDst;
            //
            maxDst = maxOrg*4;
            //
            if (arrayDst==null)
                return 0;
            if (arrayOrg==null)
                return 0;
            if (arrayDst.length < maxDst)
                return 0;
            if (arrayOrg.length < maxOrg)
                return 0;
            //
            idxDst = 0;
            for (i=0; i> 8);
                idxDst++;
                arrayDst[idxDst] = (byte)(arrayOrg[i] >> 16);
                idxDst++;
                arrayDst[idxDst] = (byte)(arrayOrg[i] >> 24);
                idxDst++;
            }
            //
            return idxDst;
        }
    
        int ByteToInt(int arrayDst[], byte arrayOrg[], int maxOrg){
            int i;
            int v;
            int idxOrg;
            int maxDst;
            //
            maxDst = maxOrg/4;
            //
            if (arrayDst==null)
                return 0;
            if (arrayOrg==null)
                return 0;
            if (arrayDst.length < maxDst)
                return 0;
            if (arrayOrg.length < maxOrg)
                return 0;
            //
            idxOrg = 0;
            for (i=0; i

提交回复
热议问题