请问,为什么输出时显示不出0x8401,而是显示的0x0001,我应该怎么修改呢,请帮忙,谢谢。

lambitionx 2009-04-12 12:37:47
int numBytes=0;
int i;
char sendBuf[] = {0x8401, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00};
for (i = 0; i < numBytes; i++)
{
printf("0x%04X ", (unsigned char)sendBuf[i]);

}
printf("\n");

请问,为什么输出时显示不出0x8401,而是显示的0x0001,我应该怎么修改呢,请帮忙,谢谢。
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"


#include <winsock2.h>
#include <stdio.h>
#include <windows.h>


#pragma comment(lib, "wsock32.lib")


const int SERVERPORT= 4007; // the port users will be connecting to
const int MYPORT=4007; // the local port
#define SERVER_IP "192.168.10.1"

void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);

//this socket is intend to use as a simple client
int _tmain()
{
int numBytes=0;
int ret=0;
int len=sizeof(SOCKADDR);
int i;

/* A Text message: LINE1 */

char sendBuf[] = {0x84, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00
};


char recvBuf[1024];

memset(recvBuf, 0, sizeof(recvBuf));

//init windows socket
init_win_socket();

SOCKET local_sock_fd;
struct sockaddr_in main_soc_addr;
struct sockaddr_in client_soc_addr;

//create the client socket
local_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(local_sock_fd ==-1)
{
puts("CREATE SOCK ERROR:");
}
//configure the client's local socket addr
client_soc_addr.sin_family = AF_INET;
client_soc_addr.sin_port = htons(MYPORT);
client_soc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(client_soc_addr.sin_zero, '\0', sizeof(client_soc_addr.sin_zero));

//bind the socket
ret = bind(local_sock_fd, (struct sockaddr *)&client_soc_addr, sizeof(client_soc_addr));
if(ret==-1)
{
puts("BIND SOCK ERROR:");
}



//configure the client's server socket addr
main_soc_addr.sin_family = AF_INET;
main_soc_addr.sin_port = htons(SERVERPORT);
main_soc_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
memset(main_soc_addr.sin_zero, '\0', sizeof(main_soc_addr.sin_zero));

//now,we can send a message to the server
numBytes =sendto(local_sock_fd, sendBuf, sizeof(sendBuf),
0, (struct sockaddr*)&main_soc_addr, sizeof(sockaddr));
if(numBytes ==-1)
{
puts("SEND DATA ERROR:");
}
else
{
puts("send a text messaging.");

for (i = 0; i < numBytes; i++)
{
printf("0x%02X ", (unsigned char)sendBuf[i]);


}


printf("\n");
}

/* recv msg */
numBytes = recvfrom(local_sock_fd,recvBuf,50,0,(SOCKADDR*)&client_soc_addr, &len);
if (numBytes == -1)
{
puts("Recv DATA ERROR:");
}
else
{

printf("Receive an ACK text message: numBytes = %d\n", numBytes);
for (i = 0; i < numBytes; i++)
{
printf("0x%02x ", (unsigned char)recvBuf[i]);


}
printf("\n");
}


//close and clean the windows sockets
close_win_socket(local_sock_fd);
clean_win_socket();

puts("any key to continue...");
getchar();
return 0;
}

//init windows socket
void init_win_socket()
{
WSADATA wdata;
if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
{
puts("WAS ERROR:");
}
}

//close windows socket
void close_win_socket(SOCKET &soc_handle)
{
if(soc_handle>0)
{
closesocket(soc_handle);
}
}

//clean windows socket
void clean_win_socket()
{
WSACleanup();
}
lambitionx 2009-04-12
  • 打赏
  • 举报
回复
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"


#include <winsock2.h>
#include <stdio.h>
#include <windows.h>



#pragma comment(lib, "wsock32.lib")



const int SERVERPORT= 4007; // the port users will be connecting to
const int MYPORT=4007; // the local port
#define SERVER_IP "192.168.10.1"

void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);

//this socket is intend to use as a simple client
int _tmain()
{
int numBytes=0;
int ret=0;
int len=sizeof(SOCKADDR);
int i;

/* A Text message: LINE1 */

char sendBuf[] = {0x8401, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00
};




char recvBuf[1024];

memset(recvBuf, 0, sizeof(recvBuf));

//init windows socket
init_win_socket();

SOCKET local_sock_fd;
struct sockaddr_in main_soc_addr;
struct sockaddr_in client_soc_addr;

//create the client socket
local_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(local_sock_fd ==-1)
{
puts("CREATE SOCK ERROR:");
}
//configure the client's local socket addr
client_soc_addr.sin_family = AF_INET;
client_soc_addr.sin_port = htons(MYPORT);
client_soc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(client_soc_addr.sin_zero, '\0', sizeof(client_soc_addr.sin_zero));

//bind the socket
ret = bind(local_sock_fd, (struct sockaddr *)&client_soc_addr, sizeof(client_soc_addr));
if(ret==-1)
{
puts("BIND SOCK ERROR:");
}



//configure the client's server socket addr
main_soc_addr.sin_family = AF_INET;
main_soc_addr.sin_port = htons(SERVERPORT);
main_soc_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
memset(main_soc_addr.sin_zero, '\0', sizeof(main_soc_addr.sin_zero));

//now,we can send a message to the server
numBytes =sendto(local_sock_fd, sendBuf, sizeof(sendBuf),
0, (struct sockaddr*)&main_soc_addr, sizeof(sockaddr));
if(numBytes ==-1)
{
puts("SEND DATA ERROR:");
}
else
{
puts("send a text messaging.");

for (i = 0; i < numBytes; i++)
{
printf("0x%04X ", (unsigned char)sendBuf[i]);


}


printf("\n");
}

/* recv msg */
numBytes = recvfrom(local_sock_fd,recvBuf,50,0,(SOCKADDR*)&client_soc_addr, &len);
if (numBytes == -1)
{
puts("Recv DATA ERROR:");
}
else
{

printf("Receive an ACK text message: numBytes = %d\n", numBytes);
for (i = 0; i < numBytes; i++)
{
printf("0x%02x ", (unsigned char)recvBuf[i]);


}
printf("\n");
}



//close and clean the windows sockets
close_win_socket(local_sock_fd);
clean_win_socket();

puts("any key to continue...");
getchar();
return 0;
}

//init windows socket
void init_win_socket()
{
WSADATA wdata;
if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
{
puts("WAS ERROR:");
}
}

//close windows socket
void close_win_socket(SOCKET &soc_handle)
{
if(soc_handle>0)
{
closesocket(soc_handle);
}
}

//clean windows socket
void clean_win_socket()
{
WSACleanup();
}


原来是这样的,是一个SOCKET程序,但显示不了0x8401。
newborn2012 2009-04-12
  • 打赏
  • 举报
回复

int numBytes=0;
int i = 0;
unsigned short sendBuf[] = {0x8401, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00};
numBytes = sizeof(sendBuf)/2;
for (i = 0; i < numBytes; i++)
{
printf("0x%04X ", (unsigned short)sendBuf[i]);

}
printf("\n");
newborn2012 2009-04-12
  • 打赏
  • 举报
回复

int numBytes=0;
int i; = 0
unsigned short sendBuf[] = {0x8401, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00};
numBytes = sizeof(sendBuf)/2;
for (i = 0; i < numBytes; i++)
{
printf("0x%04X ", (unsigned short)sendBuf[i]);

}
printf("\n");
xiaocha 2009-04-12
  • 打赏
  • 举报
回复
char sendBuf[] = ...
声明的是 char 数组,每项是一个 char,两位16进制数

而 printf("0x%04X ", ...
却要输出成四位16进制数

说清楚你要干什么!才好帮你改!
k1988 2009-04-12
  • 打赏
  • 举报
回复

int numBytes=0;
int i;
int sendBuf[] = {0x8401, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x0024,
0x1B, 0x5B, 0x31, 0x3B, 0x31, 0x48, 0x4c, 0x49, 0x4e, 0x45, 0x20, 0x31, 0x20, 0x54, 0x45,
0x58, 0x54, 0x00};
for (i = 0; i < numBytes; i++)
{
printf("0x%04X ", (int)sendBuf[i]);

}
printf("\n");

lambitionx 2009-04-12
  • 打赏
  • 举报
回复
刚学习,怎么改呢
csdn5211 2009-04-12
  • 打赏
  • 举报
回复
一个char能装下0x8401吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧