string headinfo::common_encode_url( const string &text ) { int size = text.length(); string result; // result.resize(text.length()+100); result.reserve(text.length()+100); string no_need_change_char("-_.!~*'()"); for( int i=0; i<size; i++ ) { if ( isalnum(text[i]) || no_need_change_char.find_first_of(text[i]) != string::npos ) { result.append(1,text[i]); } else if ( text[i] == ' ' ) { result.append("%20"); } else if ( (unsigned char)text[i] <= 127 ) //一般acsii 特殊字符 { result += byte_to_hex(text[i]); } else { int bytes = 0; if(((unsigned char)text[i] >= 192) && ((unsigned char)text[i] <= 223)) bytes = 2; else if(((unsigned char)text[i] > 223) && ((unsigned char)text[i] <= 239)) bytes = 3; else if(((unsigned char)text[i] > 239) && ((unsigned char)text[i] <= 247)) bytes = 4; else if(((unsigned char)text[i] > 247) && ((unsigned char)text[i] <= 251)) bytes = 5; else if(((unsigned char)text[i] > 247) && ((unsigned char)text[i] <= 251)) bytes = 6; if ( bytes == 0 || ((bytes-1)+i) > size ) assert(false); int j ; for ( j=0; j < bytes; j++ ) { result += byte_to_hex(text[i+j]); } i += j -1 ; } } return result; }
char *common_encode_uri_string(const char *string) { gchar *newURIString; gchar *hex, *tmp = NULL; int i, j, len, bytes; /* the UTF-8 string is casted to ASCII to treat the characters bytewise and convert non-ASCII compatible chars to URI hexcodes */ newURIString = g_strdup(""); len = strlen(string); for(i = 0; i < len; i++) { if(g_ascii_isalnum(string[i]) || strchr("-_.!~*'()", (int)string[i])) tmp = g_strdup_printf("%s%c", newURIString, string[i]); else if(string[i] == ' ') tmp = g_strdup_printf("%s%%20", newURIString); else if((unsigned char)string[i] <= 127) { tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex(string[i]));g_free(hex); } else { bytes = 0; if(((unsigned char)string[i] >= 192) && ((unsigned char)string[i] <= 223)) bytes = 2; else if(((unsigned char)string[i] > 223) && ((unsigned char)string[i] <= 239)) bytes = 3; else if(((unsigned char)string[i] > 239) && ((unsigned char)string[i] <= 247)) bytes = 4; else if(((unsigned char)string[i] > 247) && ((unsigned char)string[i] <= 251)) bytes = 5; else if(((unsigned char)string[i] > 247) && ((unsigned char)string[i] <= 251)) bytes = 6; if(0 != bytes) { if((i + (bytes - 1)) > len) { g_warning(("Unexpected end of character sequence or corrupt UTF-8 encoding! Some characters were dropped!")); break; } for(j=0; j < (bytes - 1); j++) { tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex((unsigned char)string[i++])); g_free(hex); g_free(newURIString); newURIString = tmp; } tmp = g_strdup_printf("%s%s", newURIString, hex = byte_to_hex((unsigned char)string[i])); g_free(hex); } else { /* sh..! */ g_error("Internal error while converting UTF-8 chars to HTTP URI!"); } } g_free(newURIString); newURIString = tmp; } return newURIString; }
void spectrum_info(u16 table[256], int verbose) { // dump range // 01234567890123 // XE: xx: xxxxcr static u08 *buf = (u08 *)"XE: xx xxxx\r\n"; static u08 *sep = (u08 *)"XE: xx ----\r\n"; u16 sum = 0; for(u08 i=0;i<255;i++) { sum += table[i]; } if(verbose) { for(u08 i=0;i<255;i++) { // draw range border if((i==B0)||(i==B1)||(i==B2)||(i==B3)) { byte_to_hex(i,sep+4); uart_send_data(sep,14); } // draw non zero values if(table[i] != 0) { byte_to_hex(i,buf+4); word_to_hex(table[i],buf+7); uart_send_data(buf,14); } } } // sort in into ranges u16 total = 0; u16 zero1Count = 0; u16 zero2Count = 0; u16 zero3Count = 0; for(u08 i=B0;i<B1;i++) { zero1Count += table[i]; } for(u08 i=B1;i<B2;i++) { zero2Count += table[i]; } for(u08 i=B2;i<B3;i++) { zero3Count += table[i]; } total = zero1Count + zero2Count + zero3Count; uart_send_string((u08 *)"XS: "); uart_send_hex_word_space(sum); uart_send_hex_word_space(total); uart_send_hex_word_space(zero1Count); uart_send_hex_word_space(zero2Count); uart_send_hex_word_crlf(zero3Count); }
bool MCSessionGenerateID(MCStringRef &r_id) { // php calculates session ids by hashing a string composed of REMOTE_ADDR, time in seconds & milliseconds, and a random value MCAutoStringRef t_remote_addr_string; char *t_remote_addr; t_remote_addr = NULL; if (MCS_getenv(MCSTR("REMOTE_ADDR"), &t_remote_addr_string)) MCCStringClone(MCStringGetCString(*t_remote_addr_string), t_remote_addr); time_t t_time; time(&t_time); MCAutoDataRef t_randombytes; // MW-2013-05-21; [[ RandomBytes ]] Use system primitive rather than SSL // directly. /* UNCHECKED */ MCU_random_bytes(64, &t_randombytes); md5_state_t t_state; md5_byte_t t_digest[16]; md5_init(&t_state); if (t_remote_addr != NULL) md5_append(&t_state, (md5_byte_t *)t_remote_addr, MCCStringLength(t_remote_addr)); md5_append(&t_state, (md5_byte_t *)&t_time, sizeof(t_time)); md5_append(&t_state, (md5_byte_t *)MCDataGetBytePtr(*t_randombytes), 64); md5_finish(&t_state, t_digest); return byte_to_hex((uint8_t*)t_digest, 16, r_id); }
int main(char* argv, int argc) { /* char c; c = getchar(); */ /*putchar(argv);*/ /* putchar(byte_to_hex(c));*/ //char *s; // %s char s; // %c printf("input string: "); scanf("%c", &s); printf("result %c\n", s); printf("result hex %c", byte_to_hex(s)); printf("\n"); return 1; }
uint32_t TDebugProtocol::writeString(const string& str) { // XXX Raw/UTF-8? string to_show = str; if (to_show.length() > (string::size_type)string_limit_) { to_show = str.substr(0, string_prefix_size_); to_show += "[...](" + boost::lexical_cast<string>(str.length()) + ")"; } string output = "\""; for (string::const_iterator it = to_show.begin(); it != to_show.end(); ++it) { if (*it == '\\') { output += "\\\\"; } else if (*it == '"') { output += "\\\""; } else if (std::isprint(*it)) { output += *it; } else { switch (*it) { case '\a': output += "\\a"; break; case '\b': output += "\\b"; break; case '\f': output += "\\f"; break; case '\n': output += "\\n"; break; case '\r': output += "\\r"; break; case '\t': output += "\\t"; break; case '\v': output += "\\v"; break; default: output += "\\x"; output += byte_to_hex(*it); } } } output += '\"'; return writeItem(output); }
uint32_t TDebugProtocol::writeByte(const int8_t byte) { return writeItem("0x" + byte_to_hex(byte)); }
static void set_result(u08 val) { byte_to_hex(val, out); out += 2; out_size += 2; }