/****************************************************************************** * FunctionName : hex2bin. * Description : convert string to bin. * Parameters : dst -- * hex_str -- * Returns : none. *******************************************************************************/ void ICACHE_FLASH_ATTR hex2bin(uint8 * dst, char * hex_str) { char * p = NULL; int i=0; for(p=hex_str; *p; p=p+2) { dst[i++] = ((char2bin(*p)) << 4) | (char2bin(*(p+1))); } }
/****************************************************************************** * FunctionName : hex2bin. * Description : convert string to bin. * Parameters : dst -- * hex_str -- * Returns : none. *******************************************************************************/ void FUNCTION_ATTRIBUTE hex2bin(uint8 * dst, char * hex_str) { char * p = NULL; int i=0; for(p=hex_str; *p; p=p+2) { dst[i++] = ((char2bin(*p)) << 4) | (char2bin(*(p+1))); } }
void hpdl_write_char(uint8_t pos, uint8_t ch) { uint8_t tkn; /* sanity check */ if(pos < 1 || pos > 4) { return; } tkn = ascii_to_hpdl(ch); /* set address; acc to datasheet should be before clearing WR. * The address follows this convention: char #1 is the leftmost, closest to * pin 1, then #2, #3 and #4 is on the outer right, like so: [1 2 3 4] * The corresponding from the datasheet is instead: [3 2 1 0] */ switch(pos) { case 1: ad |= (HPDL_A0_PIN | HPDL_A1_PIN); break; case 2: ad &= ~(HPDL_A0_PIN); ad |= (HPDL_A1_PIN); break; case 3: ad &= ~(HPDL_A1_PIN); ad |= (HPDL_A0_PIN); break; case 4: ad &= ~(HPDL_A0_PIN | HPDL_A1_PIN); break; } printf("-------Address: %s\n", char2bin(ad)); /* set writing conditions */ printf("WR low\n"); /* set digits */ printf("Digits: %s\n", char2bin(tkn)); if(tkn & (1<<6)) { printf("EP high\n"); } else { printf("EP low\n"); } /* clear writing conditions */ printf("WR high\n"); /* asm("NOP;");*/ }
int main() { char message[LENGTH] = "HelloHelloHelloHelloHelloHelloHelloHelloHelloHelo"; char key[LENGTH] = "fhseruilfhjkfdlfjojerojkgdal;.sfkodlfkheufkhjdfjk"; std::cout << "Message: "; for(int i = 0; i < LENGTH; i++) std::cout << char2bin(message[i]); std::cout << std::endl; std::cout << "Key: "; for(int i = 0; i < LENGTH; i++) std::cout << char2bin(key[i]); std::cout << std::endl; std::cout << "Encrypted: "; std::string mystring= one_time_pad(message,key,LENGTH); for(int i = 0; i < LENGTH; i++) std::cout << char2bin(mystring[i]); std::cout << std::endl; }
static int str2bin(const char* str, char bin[DEVICE_TOKEN_LEN_BIN]) { char l; char h; int i; if (strlen(str) != DEVICE_TOKEN_LEN_STR) { LM_ERR("Cannot handle device token: wrong length, return....\n"); return 0; } for( i = 0; i < DEVICE_TOKEN_LEN_BIN; ++i) { h = str[2*i]; l = str[2*i +1]; bin[i] = (char2bin(h) << 4) + char2bin(l); } return i; }
static int wlan_device_get_mac_addr(unsigned char *buf) { char macaddr[20]; int mac_len = 0; void *fp; if (!buf){ pr_err("%s, null parameter !!\n", __func__); return -EFAULT; } fp = open_image(CUSTOMER_MAC_FILE); if (fp == NULL) return -EFAULT; mac_len = get_image_block(macaddr, 17, fp); if (mac_len < 17) return -EFAULT; wlan_mac_addr[0] = (unsigned char)((char2bin(macaddr[0]) << 4) | char2bin(macaddr[1])); wlan_mac_addr[1] = (unsigned char)((char2bin(macaddr[3]) << 4) | char2bin(macaddr[4])); wlan_mac_addr[2] = (unsigned char)((char2bin(macaddr[6]) << 4) | char2bin(macaddr[7])); wlan_mac_addr[3] = (unsigned char)((char2bin(macaddr[9]) << 4) | char2bin(macaddr[10])); wlan_mac_addr[4] = (unsigned char)((char2bin(macaddr[12]) << 4) | char2bin(macaddr[13])); wlan_mac_addr[5] = (unsigned char)((char2bin(macaddr[15]) << 4) | char2bin(macaddr[16])); memcpy(buf, wlan_mac_addr, IFHWADDRLEN); pr_info("wifi mac: %x:%x:%x:%x:%x:%x\n", wlan_mac_addr[0], wlan_mac_addr[1], wlan_mac_addr[2], wlan_mac_addr[3], wlan_mac_addr[4], wlan_mac_addr[5]); close_image(fp); return 0; }