服务器:C++
客户端:JAVA
在JAVA端获得InputStream之后,把十六进制数据ab 61 56 40 8f ae 73 da 1b 60 3a 1f 2d 59 ae 93转换成字节数组后通过InputStream发出,然而C++端收到数据确是ff 61 56 40 ff ff 73 ff ff 60 ff ff ff 59 ff 93,请问大虾们这是什么原因啊,是因为JAVA中BYTE就-128到127的原因吗?在线等,谢过各位大虾了。。
十六进制转字节数组如下:
/* * 把16进制字符串转换成字节数组
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
客户端:JAVA
在JAVA端获得InputStream之后,把十六进制数据ab 61 56 40 8f ae 73 da 1b 60 3a 1f 2d 59 ae 93转换成字节数组后通过InputStream发出,然而C++端收到数据确是ff 61 56 40 ff ff 73 ff ff 60 ff ff ff 59 ff 93,请问大虾们这是什么原因啊,是因为JAVA中BYTE就-128到127的原因吗?在线等,谢过各位大虾了。。
十六进制转字节数组如下:
/* * 把16进制字符串转换成字节数组
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
解决方案 »
- cron运行log4j不能正常输出log
- 刚学java,哪位高手能指点我一下谢谢
- xml文件的插入和删除结点
- 请教!Java3D中的图形报错,请大侠帮我看看!!!!!
- InputStream为何是空指针?
- oracle中存取clob字段显示乱码
- 〈求助〉关于java和vc及jsp和asp的具体区别??
- 焦头烂额!!!在XP下怎么设置CLASSPATH.
- 请问为什么用get(i)方法不行,求帮忙看下是哪里的问题~谢谢
- 请大神帮忙一下解决: Note: further occurrences of HTTP header parsing errors will be logge
- 高分求不需要配置或者配置简单的BBS源码
- !!!!跪求高人解答,java socket传递对象,局域网可以,但是通过路由器转发,客户端服务端异常
要注意一下字节序,java是高低位存放字节数组的,而c++是从低位到高位的
对于short,int这些非单字节的类型在交互的时候需要转换一下字节序
希望对你有用
http://baike.baidu.com/view/2194385.htm
希望对你有用
http://baike.baidu.com/view/2194385.htm
就 可以通信了
{ // =============================================== bytecode to usigned number /**
*
* @param b byte
* @return short
*/
public static short b_u1(byte b)
{
return (short)((b < 0) ? ((b & 0x7F) + 0x80) : b) ;
} /**
*
* @param b byte[]
* @param offset int
* @return short
*/
public static short b_u1(byte b[], int offset)
{
return (short)((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]) ;
} // =============================================== little-endian bytecode to usigned number
// For example: Intel for Windows /**
*
* @param b byte[]
* @param offset int
* @return short
*/
public static int bb_u2(byte b[], int offset)
{
int int_a = ((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]);
int int_b = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x0100 ;
return int_a + int_b;
}
public static void setFileldAsString(String s, int index, int len, byte[] data) {
if (s != null) {
byte[] sb = s.getBytes();
for (int i = 0; i < len; i++) {
if (i < sb.length) {
data[index + i] = sb[i];
}
}
}
}
/**
*
* @param b byte[]
* @param offset int
* @return long
*/
public static long bb_u4(byte b[], int offset)
{
long l_a = ((b[offset] < 0) ? ((b[offset] & 0x7F) + 0x80) : b[offset]);
long l_b = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x0100L ;
long l_c = ((b[offset+2] < 0) ? ((b[offset+2] & 0x7F)+ 0x80) : b[offset+2]) * 0x010000L ;
long l_d = ((b[offset+3] < 0) ? ((b[offset+3] & 0x7F)+ 0x80) : b[offset+3]) * 0x01000000L ;
return l_a + l_b + l_c + l_d;
} // =============================================== big-endian bytecode to usigned number /**
*
* @param b byte[]
* @param offset int
* @return short
*/
public static int lb_u2(byte b[], int offset)
{
int int_a = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F) + 0x80) : b[offset+1]);
int int_b = ((b[offset] < 0) ? ((b[offset] & 0x7F)+ 0x80) : b[offset]) * 0x0100 ;
return int_a + int_b;
} /**
*
* @param b byte[]
* @param offset int
* @return long
*/
public static long lb_u4(byte b[], int offset)
{
long l_a = ((b[offset+3] < 0) ? ((b[offset+3] & 0x7F) + 0x80) : b[offset+3]);
long l_b = ((b[offset+2] < 0) ? ((b[offset+2] & 0x7F)+ 0x80) : b[offset+2]) * 0x0100L ;
long l_c = ((b[offset+1] < 0) ? ((b[offset+1] & 0x7F)+ 0x80) : b[offset+1]) * 0x010000L ;
long l_d = ((b[offset] < 0) ? ((b[offset] & 0x7F)+ 0x80) : b[offset]) * 0x01000000L ;
return l_a + l_b + l_c + l_d;
} // =============================================== usigned number to bytecode /**
*
* @param b byte[]
* @param offset int
* @param u1 int
*/
public static void u1_b(byte b[], int offset, int u1)
{
b[offset] = (byte)u1;
} // =============================================== usigned number to littl-endian bytecode /**
*
* @param b byte[]
* @param offset int
* @param u1 int
*/
public static void u2_bb(byte b[], int offset, int u2)
{
b[offset] = (byte)u2;
b[offset+1] = (byte)(u2 >>> 8);
} /**
*
* @param b byte[]
* @param offset int
* @param u1 int
*/
public static void u4_bb(byte b[], int offset, long u4)
{
b[offset] = (byte)u4;
b[offset+1] = (byte)(u4 >>> 8);
b[offset+2] = (byte)(u4 >>> 16);
b[offset+3] = (byte)(u4 >>> 24);
} // =============================================== usigned number to big-endian bytecode /**
*
* @param b byte[]
* @param offset int
* @param u1 int
*/
public static void u2_lb(byte b[], int offset, int u2)
{
b[offset] = (byte)(u2 >>> 8);
b[offset+1] = (byte)u2;
} /**
*
* @param b byte[]
* @param offset int
* @param u1 int
*/
public static void u4_lb(byte b[], int offset, long u4)
{
b[offset] = (byte)(u4 >>> 24);
b[offset+1] = (byte)(u4 >>> 16);
b[offset+2] = (byte)(u4 >>> 8);
b[offset+3] = (byte)u4;
}