0

I am a beginner in C++ and try to get a output from a card reader: For this i use a DLL. My problem is that my response returns a byteArray and I would like to convert it to a normal string. So my actual code currently is:

res = CT_data(ctn, &dad, &sad, 5, command, &lenr, response);
printf("\nThe Response: %s", response);

This prints to my console:

The Response: ò

But in the documentation it says that if the connection with the device works it should return 9500 or 9000!

So I think I have to transform the response! How can I do this? Thanks.

From the documentation:

nSuccess = CT_data(CT_data(ctn,dad,sad,lenc,commando,lenr,response)

ctn: Integer, 16 Bit, Unsigned (interne Terminalnummer aus CT_INIT)
dad: Integer, 8 Bit, Unsigned (Destination-Adress)
sad: Integer, 8 Bit, Unsigned (Source-Adress)
lenc: Integer 16 Bit, Unsigned ( Länge des Commandos )
commando: Byte-Array , (Kommando)
lenr: Integer 16 Bit, Unsigned (Länge der Response)
response: Byte-Array ( Antwort)
nSuccess: Integer, 8 Bit, Signed ( 0 = Erfolgreich, -1 = Fehlerhaft)

nSuccess = CT_data(ctn,dad,sad,lenc,command[],lenr,response[])  

Wenn nSuccess = 0, dann response[] prüfen. Wenn response = 9000 oder 9500 war das CT_Reset erfolgreich. Als nächstes dann die Kartenanforderung

typedef CHAR (WINAPI *CT_DATA) (USHORT, UCHAR*, UCHAR*, USHORT, UCHAR*, USHORT*, UCHAR*);
John Smith
  • 6,105
  • 16
  • 58
  • 109
  • 1
    Did you verify that `CT_data()` actually works = did you check the `res` return value? A value different from zero indicates that there was an issue and in that case the value inside `response`can be discarded as invalid. – Philip Allgaier Mar 12 '14 at 22:26
  • @PhilipAllgaier i tried to check it with `printf("\nResponse Code: " + res);` after the command but somehow it only prints to my console: `Response Code:` So i'm not 100% sure if it worked? Can you please show me how i can test it? Thanks! – John Smith Mar 12 '14 at 22:28
  • 1
    @JohnSmith `printf("\nResponse Code: %d", res);` – Roddy Mar 12 '14 at 22:29
  • Yes `res` returns `0` so this seems to work! The problem is really the format of the `response` – John Smith Mar 12 '14 at 22:32
  • 1
    You may want to provide the definitions of the other arguments involved in your function call, and some details of the called function as well (e.g. are there string buffers involved? Should they be allocated at the call site, or are they allocated by the function and returned back? Is there a buffer length specified? etc.) – Mr.C64 Mar 12 '14 at 22:35
  • @PhilipAllgaier i added parts from the documentation to my question ! I hope you can help me with this further! Thanks – John Smith Mar 12 '14 at 22:42
  • I tried `printf("\nResponse: %d", response);` and this returned `Response: 16379912` So wich format do i need? – John Smith Mar 12 '14 at 22:46
  • 1
    How did you define `response` in your code? From what you added in your post, it seems that it should be a buffer allocated by the caller, that is filled by the function. Moreover, did you pass proper length value for this buffer to the `CT_data()` function? – Mr.C64 Mar 12 '14 at 23:00
  • My problem is that i am beginner in c++! So i thought it would be better if i post the whole code: https://docs.google.com/document/d/11cZNj2nBASEo81DH7HKNHb-89J2PvMfVP8taAsX-YrE/edit?usp=sharing I hope this helps! Thanks again for your help! You are really a great person! – John Smith Mar 12 '14 at 23:12

1 Answers1

1

Could the reponse mentioned in the documentation be 2 bytes in hex? Then the response length, i.e. the return value of the CT_data() call, should be 2; and the first byte of the reponse array should be 0x90 or 0x95, i.e. 144 or 149, while the second byte should always be 0. Not sure what byte renders ò in your code page.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • maybe with the docu i added to my question you can help me further! Thanks! – John Smith Mar 12 '14 at 22:40
  • I think your right in the docu it says: `die letzten beiden Bytes von Response auswerten: 9000 = Erfolgreich` As you are german! I appreciate this! Maybe you can say me how i can select the two last 2 bytes? Thanks – John Smith Mar 12 '14 at 22:53
  • 1
    Ah, thanks for the doc. So the response length is in the int pointed to by lenr. Then the last two bytes in response hold the status bytes -- remember, indices are 0 based, i.e. lenr-1 is last: success = (response[lenr-2]==0x95||response[lenr-2]==0x90) && response[lenr-1] == 0; – Peter - Reinstate Monica Mar 12 '14 at 23:13
  • 1
    @PeterSchneider: the codepage is obviously cp850 (or cp858), where ò is at 0x95 – ninjalj Mar 12 '14 at 23:14
  • Here is my full code: https://docs.google.com/document/d/11cZNj2nBASEo81DH7HKNHb-89J2PvMfVP8taAsX-YrE/edit?usp=sharing – John Smith Mar 12 '14 at 23:14
  • @PeterSchneider thanks again for your help! I will try it tomorrow and report you my progress! Thanks – John Smith Mar 12 '14 at 23:20