Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All (Original Griff especially),

I have been asking a lot of Questions about how to parse a string that has been returned from a device, I have now figured out what to do (I hope!). It just the exact of how. I now can send and recieve low values correctly I am starting to suspect it is the way I am packing the rest of the byte as strange things happen with the magic numbers to my hex value, 255 or FF fine, 256 or 100 is read as 16.

This is due to the way the processor behaves below is the send reply from sending it 255 which I read back as 255
0c FF 00 00 00 #0
#3B025301 00000001 0
0c #FF000000

Below is the responce for the value 256 or 100
i #3B025301 00000001 0
0c #10000000

The issue is with the way the data presented I sat down with the idea that it was ( 8 bit heading (normal) 87 65 43 21 read as 21 43 65 87 which makes sense for low value as the first bit data I tried it with was 25 (base 10)
87 65 43 21: read back as: I took the headings to be 21 43 65 87
00 00 00 19 (25 in hex) 19 00 00 00 19 00 00 00

I have as I was typing this question was playing and found the problem was with the right of the value. The problem is my method for parsing the hex number is
Value_Int = int.Parse(txtValueCredit.Text, System.Globalization.NumberStyles.HexNumber);
As the unit replies with the value in a string that is eight characters long. but in an odd format the biggest the number can be is 10,000 dec or 2710 hex, I am going over the string with a sub string chopping off the zero, which could lead to problems.
Also ordering string wrong 257 is #01010000 not #10100000
hyperTerm <</cw 0c 01 01 00 00>> prog <</cw 0c 01 01 00 00>>
I think I am going nuts. As I keep seeing what I think is the problem and then finding its not.
So either the problem is I am not writing the string correctly for values over 255, but this appeared to correct the problem Converting Number to Hex with complications[^]
or I am not reading the reply correctly String builder problems....[^]
If I manually set the value to 256 I get the echo in a spy window of #00010000 If then read this back with my software I get #00010000 If I try to set 256 with my code I get #10000000 or 16
I think the problem therefor is in the sending routine ? Sorry if this is a little large and involved for a Q&A question I was usning this as white board.

1 solution

Glenn, looking at your data, I suspect it's a sending problem (and a reciveing problem)
0c FF 00 00 00 #0       This is from you to the card?
#3B025301 00000001 0    Response - not sure what it decodes to exactly, but I assume "OK" with some info.
0c #FF000000            I assume this is the return?

If so, then I would send hex 123 as:
0c 23 01 00 00 #0
as the data looks to be 32 bit little endian (rather than PC which is big endian, so the lowest byte is on the RHS)

If so, it would explain why you are having problems - is there any way you can check? Or point me at the interface specification manual?

Just sent 123(hex) as 0c 23 01 00 00 with the command /cw 0c 23 01 00 00 via HyperTerm got a #0 when I read it with a cr I get 23010000(hex) I read in as 23010000 which I tranlate to 00000123 parse to 291(dec) Meaning my reading function is OK. When I send I am sending 291 (dec) is 0c 12 30 00 00 meaning my send is wrong. I need to go back to the string parsing thanks."

If all you need is little endian <--> integer, then that's pretty easy:
public static string IntToLEString(int i)
    StringBuilder sb = new StringBuilder(4 * 2 + 3);
    sb.AppendFormat("{0:X2} ", ((i >> 00) & 0xFF));
    sb.AppendFormat("{0:X2} ", ((i >> 08) & 0xFF));
    sb.AppendFormat("{0:X2} ", ((i >> 16) & 0xFF));
    sb.AppendFormat("{0:X2} ", ((i >> 24) & 0xFF));
    return sb.ToString();
public int LEStringToInt(string s)
    int result = 0;
    string byte0 = s.Substring(0, 2);
    string byte1 = s.Substring(3, 2);
    string byte2 = s.Substring(6, 2);
    string byte3 = s.Substring(9, 2);
    result = int.Parse(byte0, NumberStyles.HexNumber) |
             int.Parse(byte1, NumberStyles.HexNumber) << 8 |
             int.Parse(byte2, NumberStyles.HexNumber) << 16 |
             int.Parse(byte3, NumberStyles.HexNumber) << 24;
    return result;
I'd probably want to put some error checking in, but you are probably doing that to recognise the response already. (You may need to ignore the spaces on the returned values as well, but that's pretty easy)
Share this answer
glennPattonWork3 29-Jan-13 9:09am    
Interface Specification jest sir. All I have is the Interface Documentation (really a PDF with some commands and responce) which really isn't enough and some PSION OPL code which is very hard to understand and is not the version running on the PSION. Customer shouting at Boss, Boss yelling via Email (he out of the country 'til next week) If you give me a web address I can upload the interface doc and the OPL code. They are in the public folder on my DropBox account ,I just can't get the web link working! or I could share them with you directly ?? Glenn I will give it another go!!
glennPattonWork3 29-Jan-13 9:16am    
The Interface Definition Document:
glennPattonWork3 29-Jan-13 9:16am    
The OPH code:

The Opl Code
glennPattonWork3 29-Jan-13 9:16am
glennPattonWork3 29-Jan-13 9:18am    
The OPL code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900