Closed cmhofmeister closed 3 years ago
One thing I can suggest is to take out the "data" array, transform it to a byte array, then reinsert the array before encoding:
CBORObject CBORArrayToByteString(CBORObject cbor) {
var count=(int)cbor.Count;
// Avoid huge memory allocation; change if needed
if(count>1000000)throw new NotSupportedException();
byte[] bytes=new byte[count];
for(var i=0;i<count;i++){
int v=cbor[i].AsInt32();
if(v<0||v>255)throw new ArgumentException();
bytes[i]=(byte)v;
}
return CBORObject.FromObject(bytes);
}
Also:
EDIT: Corrected code.
Assumed solved, closing.
The CBOR Spec (RFC7049) defines Major type 2 as a byte string. This is different than Major type 4, and array of data items.
I have an application where the client need a string transported as a byte string. In JSON the message is:
Json: {"len":1156,"off":0,"data":[48,61,66,84,54,49,48,10,49,61,10,50,61,101,56,48,51,10,51,61,57,56,51,97,10,52,61,52,48,101,50,48,49,48,48,10,53,61,48,48,10,54,61,48,48,48,48,48,48,48,48,10,55,61,51,99,48,48,48,48,48,48,10,56,61,48,49,10,57,61,48,48,10,97,61,48,48,10,98,61,49,46,49,52,46,56,10,99,61,83,82,69,81,10,100,61,67,69,51,68,50,54,52,54,69,67,65,55,10,101,61,99,57,101,51,48,49,48,48,10,102,61,48,46,48,10,49,48,61,102,100,51,54,51,55,48,48,48,48,48,48,48,48,48,48,10,49,49,61,48,48,48,48,102,101,52,50,10,49,50,61,48,48,48,48,102,101,52,50,10,49,51,61,48,48,48,48,48,48,99,51,10,49,52,61,48,48,48,48,48,48,99,51,10,49,53,61,48,48,48,48,56,48,98,102,10,49,54],"name":"/ext/params.txt"}
Converted to CBOR
A4 63 6C 65 6E 19 04 84 63 6F 66 66 00 64 64 61 74 61 98 D0 18 30 18 3D 18 42 18 54 18 36 18 31 18 30 0A 18 31 18 3D 0A 18 32 18 3D 18 65 18 38 18 30 18 33 0A 18 33 18 3D 18 39 18 38 18 33 18 61 0A 18 34 18 3D 18 34 18 30 18 65 18 32 18 30 18 31 18 30 18 30 0A 18 35 18 3D 18 30 18 30 0A 18 36 18 3D 18 30 18 30 18 30 18 30 18 30 18 30 18 30 18 30 0A 18 37 18 3D 18 33 18 63 18 30 18 30 18 30 18 30 18 30 18 30 0A 18 38 18 3D 18 30 18 31 0A 18 39 18 3D 18 30 18 30 0A 18 61 18 3D 18 30 18 30 0A 18 62 18 3D 18 31 18 2E 18 31 18 34 18 2E 18 38 0A 18 63 18 3D 18 53 18 52 18 45 18 51 0A 18 64 18 3D 18 43 18 45 18 33 18 44 18 32 18 36 18 34 18 36 18 45 18 43 18 41 18 37 0A 18 65 18 3D 18 63 18 39 18 65 18 33 18 30 18 31 18 30 18 30 0A 18 66 18 3D 18 30 18 2E 18 30 0A 18 31 18 30 18 3D 18 66 18 64 18 33 18 36 18 33 18 37 18 30 18 30 18 30 18 30 18 30 18 30 18 30 18 30 18 30 18 30 0A 18 31 18 31 18 3D 18 30 18 30 18 30 18 30 18 66 18 65 18 34 18 32 0A 18 31 18 32 18 3D 18 30 18 30 18 30 18 30 18 66 18 65 18 34 18 32 0A 18 31 18 33 18 3D 18 30 18 30 18 30 18 30 18 30 18 30 18 63 18 33 0A 18 31 18 34 18 3D 18 30 18 30 18 30 18 30 18 30 18 30 18 63 18 33 0A 18 31 18 35 18 3D 18 30 18 30 18 30 18 30 18 38 18 30 18 62 18 66 0A 18 31 18 36 64 6E 61 6D 65 6F 2F 65 78 74 2F 70 61 72 61 6D 73 2E 74 78 74
Pasting this into cbor.me the json array is being converted to a data array. (98 D0 # array(208)). I do not see an option to encode the data as a byte string. Maybe I missed it, I apologize in advance if this is the case. Would this be possible to add?