function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BarrybBarryb 

Blob as an array of bytes?

Is it possible to view the value of individual bytes in a blob? We are trying to store a series of values within a blob and then reference them by looking at a range of bytes in that blob. Any ideas? Thanks, Barry
sfdcfoxsfdcfox

You can't see a blob as bytes, but you can use EncodingUtil.base64encode on the Blob to convert it to a string of ASCII-characters that represent binary bytes (see Base64 (Wikipedia) ) for details. With some effort, you can isolate the bytes in question, convert them to binary bytes, and do whatever else you want to do.

kprkpr

Hi sfdcfox,

 

Can you show how to extract individual bytes from base64encoded Blob? I have a requirement where I need to process the bytes in this Blob in a particular style to generate a key

 

Thanks,

kpr

narsavagepnarsavagep
I needed to do this, so I created the following code:
// Converts a base64 string into a list of integers representing the encoded bytes
    public static List<Integer> B64ToBytes (String sIn) {
        Map<Integer,Integer> base64 = new Map<Integer,Integer>{65=>0,66=>1,67=>2,68=>3,69=>4,70=>5,71=>6,72=>7,73=>8,74=>9,75=>10,76=>11,77=>12,78=>13,79=>14,80=>15,81=>16,82=>17,83=>18,84=>19,85=>20,86=>21,87=>22,88=>23,89=>24,90=>25,97=>26,98=>27,99=>28,100=>29,101=>30,102=>31,103=>32,104=>33,105=>34,106=>35,107=>36,108=>37,109=>38,110=>39,111=>40,112=>41,113=>42,114=>43,115=>44,116=>45,117=>46,118=>47,119=>48,120=>49,121=>50,122=>51,48=>52,49=>53,50=>54,51=>55,52=>56,53=>57,54=>58,55=>59,56=>60,57=>61,43=>62,47=>63};

        List<Integer> lstOut = new List<Integer>();
        if ( sIn == null || sIn == '' ) return lstOut;
        
        sIn += '='.repeat( 4 - Math.mod( sIn.length(), 4) );

        for ( Integer idx=0; idx < sIn.length(); idx += 4 ) {
            if ( base64.get(sIn.charAt(idx+1)) != null ) lstOut.add( (base64.get(sIn.charAt(idx)) << 2) | (base64.get(sIn.charAt(idx+1)) >>> 4) );
            if ( base64.get(sIn.charAt(idx+2)) != null ) lstOut.add( ((base64.get(sIn.charAt(idx+1)) & 15)<<4) | (base64.get(sIn.charAt(idx+2)) >>> 2) );
            if ( base64.get(sIn.charAt(idx+3)) != null ) lstOut.add( ((base64.get(sIn.charAt(idx+2)) & 3)<<6) | base64.get(sIn.charAt(idx+3)) );
        }

        System.Debug('B64ToBytes: [' + sIn + '] = ' + lstOut);
        return lstOut;
    }//B64ToBytes

    public static List<Integer> BlobToBytes (Blob input) {
        return B64ToBytes( EncodingUtil.base64Encode(input) );
    }//BlobToBytes

Call "BlobToBytes" with a Blob, and it returns a list of integers representing the blob's bytes.
 
Raja Kar 5Raja Kar 5
Does the above code holds good for any blob to bytes which is not in UTF-8 format if you convert Blob to tostring().