+ Start a Discussion
davidwatdavidwat 

how to represent a byte array in apex?



Can someone tell me how to represent a byte array in apex?

Say i have a byte array 

byte[] vk = {1,24,45,26,75};

is there is some way to write this array in salesforce? 

please replay, the answer that solve this will be selected as a best answer. thanks
navneetnavneet

Call the decodeBase64() method and pass the parameter as string (all values with comma seperated) it will convert your input in to byte array and it returns byte array.

davidwatdavidwat

can you tell me how? i have tried those two ways and its not work for me

 

EncodingUtil.base64Decode('a','b','1'); //Method does not exist or incorrect signature

EncodingUtil.base64Decode('a,b,1');  //compiler error , is not a valid base64 charactor.

 

 

please help me out here, if have an array like byte[] k={a, b,1 };

 

how i can convert it into blob of three bytes.

navneetnavneet

I am not sure but try with following way.

 

String data='a,b,1';

Blob key = Crypto.generateAesKey(128);

 

Blob data = Blob.valueOf(data);

 

// It will encode in base 64

String b64Data = EncodingUtil.base64Encode(data);

 

// following will be use for encript and descipt 

Blob encryptedData= Crypto.encryptWithManagedIV('AES128', key, data);

Blob decryptedData = Crypto.decryptWithManagedIV('AES128', key, encryptedData); 

 

String b64Decrypted = EncodingUtil.base64Encode(decryptedData);

 

 

Navneet

ForceMantis (Amit Jain)ForceMantis (Amit Jain)
This is a very old thread but answeing this for benefit of others who may stumble upon this thread for similar issue. There is no way in salesforce to respresent a Byte array as there no such primitive data type (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_primitives.htm) like Byte in Apex, closest match is Blob but this is not Byte array.

But if you want to convert a collection of strings into a single Blob there is a way to do it in Apex which I found through research as there is no clear documentation on doing this.

Here is how you will do it.
 
List<String> sourceData = new List<String>{'a','b','c'};

String combinedDataAsHex = '';
for(String s : sourceData)
    combinedDataAsHex += EncodingUtil.convertToHex(Blob.valueOf(s));

Blob combinedDataAsBlob = EncodingUtil.convertFromHex(combinedDataAsHex);
Hope this helps!
 
Donald Dedman 21Donald Dedman 21
@forcemantis I understand there is a heap limit of 3Mb based on context.  Do you know if there is a max storage limit...32K?
BTW thank you very much for this solution...look forward to implementing it in my project