如何将java中负数转化为无符号类型32位的,与c中执行的结果不一样,请高手指点_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 如何将java中负数转化为无符号类型32位的,与c中执行的结果不一样,请高手指点

如何将java中负数转化为无符号类型32位的,与c中执行的结果不一样,请高手指点

 2010/12/27 9:45:29  ccjsjymg  http://ccjsjymg.javaeye.com  我要评论(0)
  • 摘要:下面分别是两段java和c当中的代码,其中java代码是从c中移植过来的,执行结果不一样,导致出现的原因是:在java中不存在无符号类型32位,所以导致结果不一样,请高手指点一二,谢谢!importjava.math.BigDecimal;importjava.math.BigInteger;publicclassSimLock{privatestaticfinalStringSEED="4589652157125675"
  • 标签:Java 结果 执行
下面分别是两段java和c当中的代码,其中java代码是从c中移植过来的,执行结果不一样,导致出现的原因是:在java中不存在无符号类型32位,所以导致结果不一样,请高手指点一二,谢谢!
import java.math.BigDecimal;
import java.math.BigInteger;
                                                                                               
public class SimLock {                                                                         
	private static final String SEED = "4589652157125675";                                       
	private static final int SIMLOOK_SPE_ICCID_LEN = 20;                                         
	private static final int SIMLOOK_SPE_PIC_PSW_LEN = 16;                                       
                                                                                               
	public static int SimLookGenPicPsw(String strSrc, int nSrcLen, char[]strDec, int nDecLen)   
	{               
		int srcMinLength = 0;                                                                      
		int i = 0;                                                                                 
		int j = 0;  
		
		BigDecimal data1 = new BigDecimal(0);
		BigDecimal data2 = new BigDecimal(0);
		BigDecimal decData1 = new BigDecimal(0);
		BigDecimal decData2 = new BigDecimal(0);  
		char[] vTmpSrc = new char[SIMLOOK_SPE_ICCID_LEN];
		char[] vPSW = new char[SIMLOOK_SPE_PIC_PSW_LEN];

		if ( strSrc == null )                                                    
			return -1;                                                                               
		                                                                                           
		if ( 6 > nSrcLen || SIMLOOK_SPE_PIC_PSW_LEN > nDecLen )                                    
			return -1;                                                                               
		                                                                                           
		//源字符串最小长度                                                                                                                                                                    
		srcMinLength = (strSrc.length() > nSrcLen) ? nSrcLen:strSrc.length();
		
		//不足20位前面补0                                                                          
		if ( srcMinLength < SIMLOOK_SPE_ICCID_LEN )                                                
		{                                                                                          
			for(i = 0;i < SIMLOOK_SPE_ICCID_LEN - srcMinLength;++i )                                 
			{                                                                                        
				vTmpSrc[i] = '0';                                                                     
			}                                                                                        
			                                                                                         
			for(j = 0;j < srcMinLength;++j)                                                          
			{                                                                                        
				vTmpSrc[i++] = strSrc.charAt(j);                                                              
			}                                                                                        
		}                                                                                          
		else/*超过20位的后面丢弃*/                                                                 
		{                                                                                          
			for(j = 0;j < SIMLOOK_SPE_ICCID_LEN;++j )                                                
			{                                                                                        
				vTmpSrc[j] = strSrc.charAt(j);                                                               
			}                                                                                        
		}                                                                                                                                                                                                                                    
		i = 4;/*前4 位跳过*/                                                                       
		j = 0;                                                                                     
		while(i < SIMLOOK_SPE_ICCID_LEN)                                                           
		{     
			vPSW[j] = (char)(vTmpSrc[i]^SEED.charAt(j));                                                                                                                                    
			if ( vTmpSrc[i] > '9' || vTmpSrc[i] < '0' )                                              
			{                                                                                        
				/*把非数字字符转换成数字*/                                                             
	            vPSW[j] = (char) (vTmpSrc[i]&0x07);                                              
			} 
			
			 /*转换成数值前,保证最高位非0*/                                                          
	        if (vPSW[0] == '0')                                                                  
	        {        
	            vPSW[0] = '2';                                                                   
	        }                                                                                    
	        if (vPSW[(SIMLOOK_SPE_PIC_PSW_LEN/2)] == '0')                                        
	        {                       
	            vPSW[(SIMLOOK_SPE_PIC_PSW_LEN/2)] = '6';                                         
	        }
	      
	        ++i;++j;                                                                             
		}  
		/*把16位字符串转换成两个数值*/                                                             
		i = 0;                                                                                     
		while(i<(SIMLOOK_SPE_PIC_PSW_LEN/2))                                                       
	    {                                                                                        
	        data2 = data2.multiply(new BigDecimal(10)).add(new BigDecimal(vPSW[i] - '0'));  
	        data1 = data1.multiply(new BigDecimal(10)).add(new BigDecimal(vPSW[i+(SIMLOOK_SPE_PIC_PSW_LEN/2)] - '0'));
	        i++;                                                                                 
	    }                                                                                        
		 /*对数值进行与,异或*/
	    decData1 = new BigDecimal(data1.toBigIntegerExact().and(data2.toBigIntegerExact()));
	    System.out.println(decData1);
	    decData2 = new BigDecimal(data1.toBigInteger().xor(data2.toBigInteger()));
	    i= 0;                                                                                    
	    /*再把数值转换成字符串*/
	    while(i<(SIMLOOK_SPE_PIC_PSW_LEN/2))                                                     
	    {                            
	    	srcMinLength = decData1.toBigInteger().mod(new BigInteger("10")).intValue();	
	    	strDec[i] = (char) (srcMinLength + '0');  
	        decData1 = new BigDecimal(decData1.toBigInteger().divide(new BigInteger("10")));                                                              
	    	srcMinLength = decData2.toBigInteger().mod(new BigInteger("10")).intValue();	                                              
	        strDec[i+(SIMLOOK_SPE_PIC_PSW_LEN/2)] = (char) (srcMinLength + '0');                 
	        decData2 = new BigDecimal(decData2.toBigInteger().divide(new BigInteger("10")));  
	        i++;                                                                                 
	    }
		return 1;                                                                                  
		                                                                                           
	}
	
	
} 



//c代码
#include <string.h>
#include <stdio.h>
#ifndef FALSE   /* in case these macros already exist */
#define FALSE 0  /* values of boolean */
#endif
#ifndef TRUE
#define TRUE 1
#endif

typedef signed char ascii;
typedef unsigned char           u8;
typedef unsigned long int       u32;
#define min(a, b) (a)<(b)?(a):(b)
#define SPE_SIMLOCK_TRACE printf
#define SIMLOOK_SPE_ICCID_LEN 20
#define SIMLOOK_SPE_PIC_PSW_LEN 16
static u8 SIMLOOKGenPICPSW(ascii *sSrc, u8 nSrcLen, ascii *sDec, u8 nDecLen)
{
    u8 i = 0;
    u8 l = 0;
    u8 tmp = 0;
    u8 vPSW[SIMLOOK_SPE_PIC_PSW_LEN+1] = {0};
	u8 vTmpSrc[SIMLOOK_SPE_ICCID_LEN+1] = {0};
    u32 data1 = 0, data2 = 0, decData1 = 0, decData2;
    static u8 seed[] = "4589652157125675";
    if ((!sSrc) || (!sDec)||
        (6/*最小长度为6位*/ > nSrcLen) || ((SIMLOOK_SPE_PIC_PSW_LEN+1) > nDecLen))
    {
        return -1;
    }
	tmp = strlen((const char *)sSrc);
	tmp = min(tmp, nSrcLen);
	/*不足20位前面补0*/
	if (tmp < SIMLOOK_SPE_ICCID_LEN)
	{
		for(i = 0;i<(SIMLOOK_SPE_ICCID_LEN-tmp);i++)
		{
			vTmpSrc[i] = '0';
		}
		strncpy((char*)(vTmpSrc+i), (const char*)sSrc, tmp);
	}
	else
	{
		/*超过20位的后面丢弃*/
		strncpy((char*)vTmpSrc, (const char*)sSrc, SIMLOOK_SPE_ICCID_LEN);
	}
	vTmpSrc[SIMLOOK_SPE_ICCID_LEN] = '\0';
	
    i = 4;/*前4 位跳过*/
    while(i<SIMLOOK_SPE_ICCID_LEN)
    {
        vPSW[l] = vTmpSrc[i]^seed[l];
        if ((vTmpSrc[i] > '9') || (vTmpSrc[i] < '0'))
        {
            /*把非数字字符转换成数字*/
            vPSW[l] = vTmpSrc[i]&0x07;
        }
        /*转换成数值前,保证最高位非0*/
        if (vPSW[0] == '0')
        {
            vPSW[0] = '2';
        }
        if (vPSW[(SIMLOOK_SPE_PIC_PSW_LEN/2)] == '0')
        {
            vPSW[(SIMLOOK_SPE_PIC_PSW_LEN/2)] = '6';
        }
        i++;l++;
    }
    SPE_SIMLOCK_TRACE("vPSW = %s\n", vPSW);
    i = 0;
    /*把16位字符串转换成两个数值*/
    while(i<(SIMLOOK_SPE_PIC_PSW_LEN/2))
    {
        data2 = data2*10+(vPSW[i] - '0');
        data1 = data1*10+(vPSW[i+(SIMLOOK_SPE_PIC_PSW_LEN/2)] - '0');
        i++;
    }
    /*对数值进行与,异或*/
    decData1 = data1&data2;
    decData2 = data1^data2;
    SPE_SIMLOCK_TRACE("data1 = %d, data2 = %d\n", data1, data2);
    SPE_SIMLOCK_TRACE("decData1 = %d, decData2 = %d\n", decData1, decData2);
    i= 0;
    /*再把数值转换成字符串*/
    while(i<(SIMLOOK_SPE_PIC_PSW_LEN/2))
    {
        tmp = (u8)(decData1%10);
        sDec[i] = tmp + '0';
        decData1 = decData1/10;
 
        tmp = (u8)(decData2%10);
        sDec[i+(SIMLOOK_SPE_PIC_PSW_LEN/2)] = tmp + '0';
        decData2 = decData2/10;
 
        i++;
    }
    
    sDec[SIMLOOK_SPE_PIC_PSW_LEN] = '\0';
    SPE_SIMLOCK_TRACE("----sDec = %s\n", sDec);
    return TRUE;
}

以上是两段代码,请大牛们给出意见?
发表评论
用户名: 匿名