我正在构建一个 POS 应用程序 verifone(C 语言),它应该与来自摩洛哥的 m2m 交换机进行通信,但在发送初始化消息时遇到了困难,该消息应该有一个像这样的反斜杠 (08\00)
但发送此信息时我收到的是 08\5c00
。
它通过十六进制 (5c) 中的值转换反斜杠。我使用的工具是socket workbench来模拟服务器。
如何发送反斜杠而不将其转换为 \5c
?
需要用C语言来完成。
编辑
这是我想通过 header 发送到服务器的数据,但在尝试打印 \00
时,我得到 \5C00
sprintf(data,"%s%s%s%s%s%s%s%s%s%s%s%s%s","\x30\x60\x60\x20\x15\x35\x35","\x08","\\00","\x0x00","\x01\x30\x30\x30\x30\xC0\x30\x30\x30\x30","\x97","\\00","\x30\x30","\x00\x00\x01\x00","\x02",idTerminal,idCommercant,"\x20\x20\x20\xA4\xBC");
最佳答案
如果我理解正确,你的例子的第一部分:
sprintf(data,"%s%s",
"\x30\x60\x60\x20\x15\x35\x35",
"\x08");
正在做你想做的事。问题是,在下一个 %s
上,您正在使用 "\\00"
并且您希望服务器接收 ASCII \00
(这将是 0x5c、0x30、0x30),但服务器报告它正在接收 ASCII \5c00
(这将是 0x5c、0x,35、0x43、0x30、0x30)。
我同意 Klas Lindbäck 的观点,听起来 VeriFone 终端正在做正确的事情,但服务器显示它是错误的。为了解决此问题,我会考虑做两件事,以证明这是正确的(您可以只执行其中一项,也可以同时执行两项)。
首先:您可以在发送之前使用 LOG_PRINTF(或者如果您愿意,可以打印到纸张或屏幕)来打印每个字节的值。下面是我在解决类似问题时编写的一个快速而肮脏的函数。请注意,我只关心字符串的开头(看起来就像你的情况一样),所以如果我用完缓冲区空间,我不会打印结尾。
void LogDump(unsigned char* input, int expectedLength)
{
#ifdef LOGSYS_FLAG
char buffer[100];
int idx, bfdx;
memset(buffer, 0, sizeof(buffer));
bfdx = 0;
for (idx = 0; idx < expectedLength && bfdx < sizeof(buffer); idx++)
{
//if it is a printable character, print as is
if (input[idx] > 31 && input[idx] < 127)
{
buffer[bfdx++] = (char) input[idx];
continue;
}
//if we are almost out of buffer space, show that we are truncating
// the results with a ~ character and break. Note we are leaving 5 bytes
// because we expand non-printable characters like "<121>"
if (bfdx + 5 > sizeof(buffer))
{
buffer[bfdx++] = '~';
break;
}
//if we make it here, then we have a non-printable character, so we'll show
// the value inside of "<>" to visually denote it is a numeric representation
sprintf(&buffer[bfdx], "<%d>", (int) input[idx]);
//advance bfdx to the next 0 in buffer. It will be at least 3...
bfdx += 3;
//... but for 2 and 3 digit numbers, it will be more.
while (buffer[bfdx] > 0)
bfdx++;
}
//I like to surround my LOG_PRINTF statements with short waits because if there
// is a crash in the program directly after this call, the LOG_PRINTF will not
// finish writing to the serial port and that can make it look like this LOG_PRINTF
// never executed which can make it look like the problem is elsewhere
SVC_WAIT(5);
LOG_PRINTF(("%s", buffer));
SVC_WAIT(5);
#endif
}
第二:尝试为 char 数组中的每个位置分配一个显式值。如果您已经使用了我上面的LOG_PRINTF
建议,并且发现它没有发送您认为应该发送的内容,那么这将是修复它的一种方法,以便它确实发送您想要的内容。这个方法有点乏味,但既然你要拼写出每个值,无论如何,它不应该有太多的开销:
data[0] = 0x30;
//actually, I'd probably use either the decimal value: data[0] = 48;
// or I'd use the ASCII value: data[0] = '0';
// depending on what this data actually represents, either of those is
// likely to be more clear to whomever has to read the code later.
// However, that's your call to make.
data[1] = 0x60;
data[2] = 0x60;
data[3] = 0x20;
data[4] = 0x15;
data[5] = 0x35;
data[6] = 0x35;
data[7] = 0x08;
data[8] = 0x5C; // This is the '\'
data[9] = 0x48; // The first '0'
data[10]= 0x48; // The second '0'
data[11]= 0;
//for starters, you may want to stop here and see what you get on the other side
在您向自己证明是或不是导致问题的 VeriFone 代码后,您就会知道是否需要关注终端或服务器端。
关于c - 使用 C 套接字发送反斜杠以获取 iso8583 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28430510/