1

I have a java line of code that I want to convert to typescript.

NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/vcard".getBytes(), new byte[] {}, messagebytes);

NdefRecord is a function that takes the following parameters: (tnf:number,type:string|number[],id:string|number[],payload:string|number[])

I tried several approaches to writing getBytes alternatives in typescript:

var vcard= `BEGIN:VCARD\nVERSION:2.1\nN:;${firstName}\nORG: ${org}\nEMAIL:${email}\nEND:VCARD`;
var payload = this.nfc.stringToBytes(vcard)
var encoder = new TextEncoder();
var array = Array.from(encoder.encode("text/vcard"))

1:

record = this.ndef.record(tnf, array, [], payload); 

2:

record = this.ndef.record(tnf, encoder.encode("text/vcard").toString(), [], payload);

3:

record = this.ndef.record(tnf, this.nfc.stringToBytes("text/vcard"), [], payload); 

The first and third approaches yield a JSON error, whereas the second returns the contents of the vcard as a string. Is there any way to get the bytes of the record type "text/vcard"?

Rawan
  • 11
  • 2
  • 1
    I don't know Java, but a quick search suggest that Java's `string.getBytes()` does something similar to JS's `[...string].map(char => char.charCodeAt())`. – InSync May 22 '23 at 05:55
  • `Array.from(encoder.encode("text/vcard"))` [should work in order to get the byte array out of that string](https://jsbin.com/nuhodudite/edit?js,console). I'm not sure where and how exactly you experience a problem with it. – VLAZ May 22 '23 at 06:23
  • There is already a question and answers posted to this topic. How to convert a string to Bytearray in javascript. Check this solution https://stackoverflow.com/a/48762658/5012127 – Mehyar Sawas May 22 '23 at 08:39

0 Answers0