FORCEINLINE BOOL StringToHex(LPCTSTR HexString, PUCHAR HexValue) { UCHAR lower, upper; if (!CharToHex(HexString[0], &upper)) return FALSE; if (!CharToHex(HexString[1], &lower)) return FALSE; *HexValue = static_cast<UCHAR>((upper << 4) | lower); return TRUE; }
/******************************************************************************************** > INT32 AIBitmapProcessor::DecodeHexStringAsRGB( AI5EPSFilter& filter, ADDR& pCurrentLine, INT32& nLineOffset, INT32 nLineLength, INT32 nMaxLineOffset, UINT32 nStart, BYTE RGBVal[], UINT32& nShift ) Author: Colin_Barfoot (Xara Group Ltd) <*****@*****.**> Created: 23/03/00 Purpose: Treat the current token as a hex string, and decode it into a BGR bitmap. Inputs: filter the filter doing the import nLineLength the maximum number of bytes to read from the hex string. nMaxLineOffset the width of the bitmap - 1 nStart where in TokenBuf (the member that contains the semantic value of the token) to start the decoding in,out: pCurrentLine the line in the bitmap to place the binary data into nLineOffset the offset into the current line that will be read next RGBVal the accumulated RGB value nShift the index into RGBVal giving the next value to read Returns: If an error occurs (i.e. invalid hex data) then -1 is returned. This is way too complicated but I copied the code from the base class and worked from there. How about read the bitmap line by line getting an RGB value from the appropriate "BitmapParser", say. ********************************************************************************************/ INT32 AIBitmapProcessor::DecodeHexStringAsRGB( AI5EPSFilter& filter, ADDR& pCurrentLine, INT32& nLineOffset, INT32 nLineLength, INT32 nMaxLineOffset, UINT32 nStart, BYTE RGBVal[], UINT32& nShift ) { UINT32 nTokenLen = camStrlen(filter.GetTokenBuf() + nStart); // Assume hex strings are even-numbered in length for the moment if ( (nTokenLen & 1) != 0 ) { TRACE( _T("Bad hex string length in DecodeHexString\n") ); return -1; } ///////////////// // Decode the string two characters at a time ///////////////// UINT32 i; for ( i = nStart; i < nTokenLen; i += 2 ) { RGBVal[nShift] = 0; if ( !CharToHex( filter.GetTokenBuf()[i], RGBVal[nShift] ) ) return -1; RGBVal[nShift] <<= 4; if ( !CharToHex( filter.GetTokenBuf()[i + 1], RGBVal[nShift] ) ) return -1; ++nShift; /////////////////// // once we have a complete RGB value, stick it in the bitmap // reversing it to BGR /////////////////// if ( nShift > 2 ) { pCurrentLine[nLineOffset++] = RGBVal[2]; pCurrentLine[nLineOffset++] = RGBVal[1]; pCurrentLine[nLineOffset++] = RGBVal[0]; if ( nLineOffset > nMaxLineOffset ) { pCurrentLine -= nLineLength; nLineOffset = 0; } nShift = 0; } } // How much data did we read? return i; }
int wxURI::DecodeEscape(wxString::const_iterator& i) { int hi = CharToHex(*++i); if ( hi == -1 ) return -1; int lo = CharToHex(*++i); if ( lo == -1 ) return -1; return (hi << 4) | lo; }
void writeHuffTable( Groundfloor::FileWriter *fw, const Groundfloor::String *name, const Groundfloor::String *table, unsigned int pass ) { char crlf[] = GFNATIVENEXTLINE; const char *p = table->getValue(); unsigned int c = table->getLength(); Groundfloor::BValue lenval, passval; lenval.setInteger(c); passval.setInteger(pass); Groundfloor::String txt; txt << "const unsigned int " << name << "_hufftablesize" << passval.asString() << " = " << lenval.asString() << ";" << crlf; txt << "const unsigned char " << name << "_hufftable" << passval.asString() << "[] = " << crlf << "{"; for ( unsigned int i = 0; i < c; i++ ) { if ( i != 0 ) { txt << ","; if ( i % 20 == 0 ) { txt << crlf; } } txt << "0x"; CharToHex( p[i], &txt ); } txt << "};" << crlf; fw->add( &txt ); }
/* Pre-Reqs: setupI2C1(), setupUART() Desc: Prints a given char to the LCD screen. If the current line is full wraps around to the next line by calling LSDShftLine() Inputs: char ch - Character to print to LCD */ void PrintToLCD(char ch){ uint32_t i2caddress = LCDADDR; uint8_t lcd_cmd[2] = {0x00, 0x80}; uint8_t lcd_crsr[1] = {0x14}; int validChar = 1; uint8_t lcd_print[2] = {0x40, CharToHex(ch)}; dataPtr = lcd_print; if (lcd_print[1]==NULL) { validChar = 0; } if (validChar){ // sendToI2C1(i2caddress, lcd_cmd, 2); sendToI2C1(i2caddress, dataPtr, 2); sendToI2C1(i2caddress, lcd_crsr, 1); //Check if EOL on LCD numChars++; if(numChars == 16){ LCDShftLine(); if(currline == 1){ currline = 2; }else if(currline == 2){ currline = 1; SystickTimer(1600); ClearLCD(); } } } }
UnicodeString TCopyParamType::GetLogStr() const { wchar_t CaseC[] = L"NULFS"; wchar_t ModeC[] = L"BAM"; wchar_t ResumeC[] = L"YSN"; // OpenArray (ARRAYOFCONST) supports only up to 19 arguments, so we had to split it return FORMAT( L" PrTime: %s%s; PrRO: %s; Rght: %s; PrR: %s (%s); FnCs: %c; RIC: %c; " L"Resume: %c (%d); CalcS: %s; Mask: %s\n", BooleanToEngStr(GetPreserveTime()).c_str(), UnicodeString(GetPreserveTime() && GetPreserveTimeDirs() ? L"+Dirs" : L"").c_str(), BooleanToEngStr(GetPreserveReadOnly()).c_str(), GetRights().GetText().c_str(), BooleanToEngStr(GetPreserveRights()).c_str(), BooleanToEngStr(GetIgnorePermErrors()).c_str(), CaseC[GetFileNameCase()], CharToHex(GetInvalidCharsReplacement()).c_str(), ResumeC[GetResumeSupport()], (int)GetResumeThreshold(), BooleanToEngStr(GetCalculateSize()).c_str(), GetFileMask().c_str()) + FORMAT( L" TM: %c; ClAr: %s; RemEOF: %s; RemBOM: %s; CPS: %u; NewerOnly: %s; InclM: %s; ResumeL: %d\n" L" AscM: %s\n", ModeC[GetTransferMode()], BooleanToEngStr(GetClearArchive()).c_str(), BooleanToEngStr(GetRemoveCtrlZ()).c_str(), BooleanToEngStr(GetRemoveBOM()).c_str(), int(GetCPSLimit()), BooleanToEngStr(GetNewerOnly()).c_str(), GetIncludeFileMask().GetMasks().c_str(), ((FTransferSkipList.get() != nullptr) ? FTransferSkipList->GetCount() : 0) + (!FTransferResumeFile.IsEmpty() ? 1 : 0), GetAsciiFileMask().GetMasks().c_str()); }
/* *@功能:蓝牙MAC地址查询 * * 将MAC地址写入外部flash中 */ uint8_t bluetooth_mac(void) { uint8_t j=0,t=0,count=0; uint8_t try_num=1; uint8_t char_buf[100]; uint8_t MAC[6]; uint8_t MAC_NULL[6]={0,0,0,0,0,0}; UART3Reset(3); for(t=0;t<try_num;t++) //至多尝试3次 { UART3nSendChar(3,"TTM:RST-SYSTEMRESET",20); //蓝牙串口AT指令 delay_ms(50); UART3nSendChar(3,"TTM:MAC-?",10); //蓝牙串口AT指令 delay_ms(50); //延时等待蓝牙回复 count=UART3GetCount(3); UART3nReadChar(3,char_buf,count); //读缓冲区数据到char_buf //for debug // UART1nSendChar(1,char_buf,count); //发送读到的所有数据 // delay_ms(100); for(j=0;j<count-1;j++) //从for循环退出的话,j=buf_len-1 if(char_buf[j]=='0'&&char_buf[j+1]=='x') break; //从break退出的话,j<buf_len-1 if(j<count-1-12) //如果提前成功 { //for debug //UART1nSendChar(1,char_buf+j,12); //已字符形式发送MAC地址 //delay_ms(100); j=j+2; CharToHex(char_buf+j,MAC,12); SPI_FLASH_Write(MAC,BLUETOOTH_MAC,6); return 1;//不用再尝试了 } } //失败 if(t>=try_num) { SPI_FLASH_Write(MAC_NULL,BLUETOOTH_MAC,6); return 0; } }
//--------------------------------------------------------------------------- UnicodeString StrToHex(const UnicodeString & Str, bool UpperCase, char Separator) { UnicodeString Result; for (intptr_t I = 1; I <= Str.Length(); I++) { Result += CharToHex(static_cast<char>(Str[I]), UpperCase); if ((Separator != L'\0') && (I <= Str.Length())) { Result += Separator; } } return Result; }
BOOL ParseNdasDeviceId( __in LPCTSTR Argument, __out PNDAS_DEVICE_IDENTIFIER NdasDeviceIdentifier) { LPCTSTR p = Argument; for (DWORD i = 0; i < 5; ++i) { if (NULL == *p || NULL == *(p+1)) return FALSE; BOOL success = StringToHex(p, &NdasDeviceIdentifier->Identifier[i]); if (!success) return FALSE; p += 2; if (_T(':') == *p || _T('-') == *p) ++p; } if (NULL == *p || NULL == *(p+1)) return FALSE; BOOL success = StringToHex(p, &NdasDeviceIdentifier->Identifier[5]); if (!success) return FALSE; p += 2; if (_T(':') == *p || _T('-') == *p) { ++p; success = CharToHex(*p, &NdasDeviceIdentifier->UnitNumber); if (!success) { return FALSE; } ++p; } else { NdasDeviceIdentifier->UnitNumber = 0; } if (NULL != *p) { // redundant characters return FALSE; } return TRUE; }
//url code 编码 std::string EASY_UTIL::Encode(const std::string & sSrc) { std::string result; std::string::const_iterator pos; for(pos = sSrc.begin(); pos != sSrc.end(); ++pos) { if ( *pos >= '0' && *pos <= '9' || *pos >= 'A' && *pos <= 'Z' || *pos >= 'a' && *pos <= 'z' ) { result.append(1, *pos); } else { result.append("%" + CharToHex(*pos)); } } return result; }
/* Pre-Reqs: CharToHex(), sendToI2C1() Desc: Copy of PrintToLCD() but with no reset of lcd on line end or screen end Inputs: char to be displayed */ void PrintToLCD2(char ch){ uint32_t i2caddress = LCDADDR; uint8_t lcd_cmd[2] = {0x00, 0x80}; uint8_t lcd_crsr[1] = {0x14}; int validChar = 1; uint8_t lcd_print[2] = {0x40, CharToHex(ch)}; dataPtr = lcd_print; if (lcd_print[1]==NULL) { validChar = 0; } if (validChar){ // sendToI2C1(i2caddress, lcd_cmd, 2); sendToI2C1(i2caddress, dataPtr, 2); sendToI2C1(i2caddress, lcd_crsr, 1); } }
std::string CCharset::HexToStr(const std::string &strData) { std::string strRet; strRet.assign(( strData.size() + 1 ) / 2, '\0'); for ( size_t i = 0; i < strData.size(); i++ ) { int m = CharToHex( strData[i] ); if ( m >= 0 ) { if ( i%2 == 0 ) { strRet[i/2] |= (m & 0xf) * 16; } else { strRet[i/2] |= (m & 0xf); } } } return strRet; }
string UrlEncode(const string &url) { string result; string::const_iterator iter; for(iter = url.begin(); iter != url.end(); ++iter) { switch(*iter) { case ' ': result.append(1, '+'); break; // alnum case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': // mark case '-': case '_': case '.': case '!': case '~': case '*': case '\'': case '(': case ')': result.append(1, *iter); break; // escape default: result.append(1, '%'); result.append(CharToHex(*iter)); break; } } return result; }
int CComDev::RecvData() { unsigned long nRead = 0; char buff[BUFFERSIZE]; bzero(buff, sizeof(buff)); char cShow[BUFFERSIZE]; bzero(cShow, sizeof(buff)); while((nRead = read(m_nFd, buff, sizeof(buff))) > 0) { memcpy(m_buff + m_nBuffLength, buff, nRead); m_nBuffLength += nRead; } m_buff[m_nBuffLength] = '\0'; printf("RECV CHAR:%s\n", m_buff); if (CharToHex(cShow, BUFFERSIZE, m_buff, m_nBuffLength) == TRUE) { printf("RECV HEX:%s\n", cShow); } else { printf("\nCharToHex:Error!"); } //存入缓存 或通知 待加 m_RecvDevDataFun(m_object,m_cDev, m_buff, m_nBuffLength); m_nBuffLength = 0; return TRUE; }
int main(int argc, char *argv[]) { int input=6, output=6; char octal_buff[SIZE]; char hexa_buff[SIZE]; char ascii_buff[SIZE]; char char_buff[SIZE]; char buffer[SIZE]; while (input > 5) { puts("-------------------------------------------------------"); puts("\tSelect input:\t"); puts("\t\t1 - Octal"); puts("\t\t2 - Hexadecimal"); puts("\t\t3 - ASCII Char"); puts("\t\t4 - Char\n"); puts("-------------------------------------------------------"); printf("Choice Input: "); scanf("%d",&input); EmptyBuffer(); } while (output > 5) { puts("-------------------------------------------------------"); puts("\tSelect output:\t"); puts("\t\t1 - Octal"); puts("\t\t2 - Hexadecimal"); puts("\t\t3 - ASCII Char"); puts("\t\t4 - Char\n"); puts("-------------------------------------------------------"); printf("Choice Output: "); scanf("%d",&output); EmptyBuffer(); } //Octal Input if(input ==1) { if(output == 1) { puts("Bitche I do not convert it !"); } printf("Input octal: "); read(buffer); strcpy(octal_buff,buffer); if(output==2) { OcToHex(octal_buff,hexa_buff); } if(output==3) { OcToAscii(octal_buff,ascii_buff); } if(output==4) { OcToChar(octal_buff,char_buff); } } //Hexadecimal Input if(input == 2) { if(output==2) { puts("You want to suck ?"); } printf("Input Hexadecimal: "); read(buffer); strcpy(hexa_buff,buffer); if(output==1) { HexToOct(hexa_buff,octal_buff); } if(output==3) { HexToAscii(hexa_buff,ascii_buff); } if(output==4) { HexToChar(hexa_buff,char_buff); } } //Ascii Input if(input == 3) { if(output==3) { puts("Kill yourself -->[-]"); } printf("Input Ascii: "); read(buffer); strcpy(ascii_buff,buffer); if(output==1) { AsciiToOct(ascii_buff,octal_buff); } if(output==2) { AsciiToHex(ascii_buff,hexa_buff); } if(output==4) { AsciiToChar(ascii_buff,char_buff); } } //Char Input if(input == 4) { if(output==4) { puts("Exile you in /dev/null !!!"); } printf("Input Char: "); read(buffer); strcpy(char_buff,buffer); if(output==1) { CharToOct(char_buff,octal_buff); } if(output==2) { CharToHex(char_buff,hexa_buff); } if(output==3) { CharToAscii(char_buff,ascii_buff); } } return 0; }
/******************************************************************************************** > INT32 AIBitmapProcessor::DecodeHexStringAsCMYK( AI5EPSFilter& filter, ADDR& pCurrentLine, INT32& nLineOffset, INT32 nLineLength, INT32 nMaxLineOffset, UINT32 nStart, BYTE CMYKval[], UINT32& nShift ) Author: Colin_Barfoot (Xara Group Ltd) <*****@*****.**> Created: 23/03/00 Purpose: Treat the current token as a hex string, and decode it into a BGR bitmap. Inputs: filter the filter doing the import nLineLength the maximum number of bytes to read from the hex string. nMaxLineOffset the width of the bitmap - 1 nStart where in TokenBuf (the member that contains the semantic value of the token) to start the decoding in,out: pCurrentLine the line in the bitmap to place the binary data into nLineOffset the offset into the current line that will be read next CMYKval the accumulated CMYK value nShift the index into CMYKVal giving the next value to read Returns: If an error occurs (i.e. invalid hex data) then -1 is returned. This is way too complicated but I copied the code from the base class and worked from there. How about read the bitmap line by line getting an RGB value from the appropriate "BitmapParser", say. ********************************************************************************************/ INT32 AIBitmapProcessor::DecodeHexStringAsCMYK( AI5EPSFilter& filter, ADDR& pCurrentLine, INT32& nLineOffset, INT32 nLineLength, INT32 nMaxLineOffset, UINT32 nStart, BYTE CMYKval[], UINT32& nShift ) { UINT32 nTokenLen = camStrlen(filter.GetTokenBuf() + nStart); // Assume hex strings are even-numbered in length for the moment if ( (nTokenLen & 1) != 0 ) { TRACE( _T("Bad hex string length in DecodeHexString\n") ); return -1; } ///////////////// // Decode the string two characters at a time keeping a running CMYK value ///////////////// double dRed, dGreen, dBlue; double dKey; UINT32 i; for ( i = nStart; i < nTokenLen; i += 2 ) { ///////////////// // read in the CMYK value one component at at time // first clear the next value /////////////////// CMYKval[nShift] = 0; if ( !CharToHex( filter.GetTokenBuf()[i], CMYKval[nShift] ) ) return -1; CMYKval[nShift] <<= 4; if ( !CharToHex( filter.GetTokenBuf()[i + 1], CMYKval[nShift] ) ) return -1; ++nShift; /////////////////// // once we have a complete CMYK value, stick it in the bitmap /////////////////// if ( nShift > 3 ) { ///////////////// // Convert the colour into an RGB format. ///////////////// dKey = double(CMYKval[3]) / 255.0; dRed = 1.0 - (min(1.0, (double(CMYKval[0]) / 255.0 + dKey ))); dGreen = 1.0 - (min(1.0, (double(CMYKval[1]) / 255.0 + dKey ))); dBlue = 1.0 - (min(1.0, (double(CMYKval[2]) / 255.0 + dKey ))); ///////////////// // Set the value for the current pixel in the bitmap. ///////////////// pCurrentLine[nLineOffset++] = ColourValueToByte( dBlue ); pCurrentLine[nLineOffset++] = ColourValueToByte( dGreen ); pCurrentLine[nLineOffset++] = ColourValueToByte( dRed ); if ( nLineOffset > nMaxLineOffset ) { pCurrentLine -= nLineLength; nLineOffset = 0; } ///////////////// // start the next CMYK value ///////////////// nShift = 0; } } // How much data did we read? return i; }
wxChar wxURI::TranslateEscape(const wxChar* s) { wxASSERT_MSG( IsHex(s[0]) && IsHex(s[1]), wxT("Invalid escape sequence!")); return wx_truncate_cast(wxChar, (CharToHex(s[0]) << 4 ) | CharToHex(s[1])); }