# -*- coding: utf-8 -*-
"""
Created on Thu Jun 01 10:14:08 2017

@author: e1501141
"""
import socket
import time
from robotino import*
def ConversionUdp(ID_Message,int0,int1,int2,int3,int4,int5,int6,int7):
    TAB_BYTE_HEX =[ '0', '1', '2', '3', '4', '5', '6','7','8', '9', 'a', 'b', 'c', 'd', 'e','f' ]        
    CH_int=[int0,int1,int2,int3,int4,int5,int6,int7]
    i=0    
    somme=36
    Valeurs_entrees=""
    
    if ID_Message==0:
        chaine="002400"
    if  ID_Message==1:
        chaine="012400"
        somme+=1   
    
    
    while(i<8) :
        if (CH_int[i]<0):
            CH_int[i]=(256*256*256*256+ CH_int[i])
            
        tampon_Byte3=(CH_int[i]/(256*256*256))
        somme+=tampon_Byte3
                    
        CH_int[i]=CH_int[i]%(256*256*256);
        tampon_Byte2=(CH_int[i]/(256*256))
        somme+=tampon_Byte2
                    
        CH_int[i]=CH_int[i]%(256*256);
        tampon_Byte1=(CH_int[i]/(256))
        somme+=tampon_Byte1
                
        CH_int[i]=CH_int[i]%256
        tampon_Byte0=(CH_int[i])
        somme+=tampon_Byte0
        
       
        tampon_chaine=  TAB_BYTE_HEX[tampon_Byte0/16]+TAB_BYTE_HEX[tampon_Byte0%16]+ TAB_BYTE_HEX[tampon_Byte1/16]+TAB_BYTE_HEX[tampon_Byte1%16]+ TAB_BYTE_HEX[tampon_Byte2/16]+TAB_BYTE_HEX[tampon_Byte2%16]+ TAB_BYTE_HEX[tampon_Byte3/16]+TAB_BYTE_HEX[tampon_Byte3%16];
        Valeurs_entrees+=tampon_chaine
        i=i+1
    
    somme= (255-(somme%256))    
    chaine+=TAB_BYTE_HEX[(somme/16)]+TAB_BYTE_HEX[(somme%16)]+Valeurs_entrees
    return chaine.decode("hex")
        
def Conversion_en_INT(String_a_Massacrer,int_a_retourner):  
    Octet_Depart=4+4*int_a_retourner
    String_a_Massacrer = String_a_Massacrer[Octet_Depart:len(String_a_Massacrer)]
    Int_encours =0
    Char_Tampon3=String_a_Massacrer[0]
    Char_Tampon2=String_a_Massacrer[1]
    Char_Tampon1=String_a_Massacrer[2]
    Char_Tampon0=String_a_Massacrer[3]
    if ord(Char_Tampon0)>128:
        Int_encours=-((256*256*256*256) -(ord(Char_Tampon3)+(256*ord(Char_Tampon2)) +(256*256*ord(Char_Tampon1)+ (256*256*256*ord(Char_Tampon0)))))
    else:
        Int_encours=ord(Char_Tampon3)+(256*ord(Char_Tampon2)) +(256*256*ord(Char_Tampon1)+ (256*256*256*ord(Char_Tampon0)))
    #print "Int en cours: ", Int_encours    
    return Int_encours


