cancel
Showing results for 
Search instead for 
Did you mean: 

How to fill a PB byte array with negative byte values

Former Member
0 Kudos

Hello Community,

I'm using PB 12.1 6639

I have a datatype related problem.

I have to AES-encrypt a Byte Array. For encrypting i use a third Party activeX (Crypt2 from Chilkat Software).

There is a function byte[] EncryptBytes(Byte data[]).

The value i have to crypt is a Long encoded as a signed two complement (big endian) (=Java Long).

For example a Long value of 102400 is stored in a Byte Array in that way:

Byte[1] = 1

Byte[2] = -112

Byte[3] = 0

And here starts the Trouble. PB's Byte datatype is a unsigned datatype and can hold only positive Byte values.

I also tried using a blob variable instead of a Byte Array (the function EncryptBytes takes it without error), but on Setting up

the data in the blob variable using Setbyte(blob, n, byte)-function there is the same problem that Setbyte only can handle positive Byte values.

So the question is how to incorporate negative Byte values into a Byte Array or a blob!

Hope somebody can help me.

Regards Martin

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Martin,

-112 = 0xFF90 in a WORD or 0x90 in a byte.

0x90 is 144, so you can try byte[2] = 144 or byte[2] = 256 + -112).

Regards

Bernhard

Former Member
0 Kudos

Thank you Chris, thank you Bernhard! @Bernhard: maybe I've not understand well what you mean, but on Setting Byte[2] to a value doesn't effect Byte[1], so there are still only postive values in the Array @chris: I'm also not sure what you mean by 'manipulate each signed byte', but maybe your comment lead me to a solution, that I think is working: the Magic word is Blobedit(). blob lbl_data Byte lbytes[] lbytes[1] = 0 lbl_data = Blob(lbytes) //initalialize the blob, for simplicity only 1 byte // edit the value in Byte one ll_rc = Blobedit(lbl_data, 1, 0)      // Shows "0" Messagebox("", Long(lbl_data)) ll_rc = Blobedit(lbl_data, 1, 144) Messagebox("", Long(lbl_data))        // Shows "144" ll_rc = Blobedit(lbl_data, 1, -112) Messagebox("", Long(lbl_data))        // Shows "-112" you can see, that the conversion of a Byte in the blob back to a Long value Shows the correct original value. Do you agree, that my Interpretation is correct?

Former Member
0 Kudos

Sorry for bad formatting of my post, but I don't know how to use this Editor (wrote in paragraphs, but on 'add reply' formatting disappears ...)

Former Member
0 Kudos

Yes, sorry <my bad> - I should have said it clearer </my bad> ... manipulate each byte in the blob to the correct signage!  

Correct, the BlobEdit ( ) method would be the key to each byte's bit values. 

Former Member
0 Kudos

Hi Martin,

ll_Value = 102400

lblb_data = blob( space( 2 ))    // 4byte

BlobEdit( lblb_Data, 1, ll_Value )

lby_Data[1] = byte( BlobMid( lblb_Data, 1, 1 ))     // = 0

lby_Data[2] = byte( BlobMid( lblb_Data, 2, 1 ))     // = 144

lby_Data[3] = byte( BlobMid( lblb_Data, 3, 1 ))     // = 1

lby_Data[4] = byte( BlobMid( lblb_Data, 4, 1 ))     // = 0

To get 1, 144, 0 you can use:

lby_Data[1] = byte( BlobMid( lblb_Data, 3, 1 ))     // = 1

lby_Data[2] = byte( BlobMid( lblb_Data, 2, 1 ))     // = 144

lby_Data[3] = byte( BlobMid( lblb_Data, 1, 1 ))     // = 0

Regards

Bernhard

Former Member
0 Kudos

Hi Martin;

  A very interesting question! One aspect of PB that I have not played with (negative byte) but I see your issue.

  I wonder if the approach might be to use the BLOB data type as the PB byte array and then manipulate each signed byte in the blob using the Two's Compliment?

Regards ... Chris