Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > Algorithms

Reply
 
Thread Tools

output in HEX format
Old 29.05.2004, 10:37   #1
Академик
 
greka's Avatar
 
Join Date: 09 2001
Location: inside myself
Posts: 5,369
Rep Power: 6
Wink output in HEX format

I wonder if some needs such utility.
This function allows to present (print) binary data stream in hexadecimal format.

PHP Code:
void OutputInHexFormat(char *pBufint iLen)
{
#define OUTPUTWIDTH        50
    
static char outputBuf[300];
    
int iintCtriLenStr;

    
char tmp[20];
    
char *pCursor;

    
memset(outputBuf0sizeof(outputBuf));
    
pCursor outputBuf;    

    
intCtr 0;
    for (
0iLeni++)
    {
        
sprintf(tmp"%02x "pBuf[i] & (int)0xFF);
        
iLenStr strlen(tmp);
        
intCtr += iLenStr;

        
strcat(pCursortmp); // append new data
        
pCursor += iLenStr// advance destination pointer

        // check the output width:
        
if ( ( pCursor outputBuf ) > OUTPUTWIDTH-)
        {
            
printf(" %s\n"outputBuf );
            
pCursor outputBuf// reset to the begin
            // prepare the storage:
            
memset(outputBuf0intCtr*sizeof(char) );
        }
    }

    
printf(" %s\n"outputBuf );

To make it faster use table of strings {"00", "01", "02", ..., "ff "} instead of constructing string each time:
PHP Code:
sprintf(tmp"%02x "pBuf[i] & (int)0xFF); 
also replace this call
PHP Code:
iLenStr strlen(tmp); 
to
PHP Code:
iLenStr 3
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/

Old 29.05.2004, 11:39   #2
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
Default

1. Зачем & в sprintf - е. Массив так и так байтов, больше 255 не будет.
PHP Code:
#include <sstream>
#include <iomanip>
#include <vector>
#include <iostream>
#include <iterator>

void printHex(const char pBuf, const int iLen, const int iWidthstd::ostream os) {
    
std::ostringstream ss;
    for (
int i 0iLen; ++i)
        
ss << std::hex << std::setw(2) << std::setfill('0') << (int)pBuf[i];
    const 
std::string s ss.str();
    const 
int iRowCount s.length() / iWidth 1;
    for (
int i 0iRowCount; ++i)
      
os << s.substr(iWidthiWidth) << std::endl//must not crash on standard compliant string realization.
}

int main() {
    
//read as much as we can
    
std::vector<charv((std::istream_iterator<char>(std::cin)), std::istream_iterator<char>());    
    
printHex(&v.front(), v.size(), 78std::cout); //standart output width
    
return 0;

мое решение, но Ц++
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!

Old 29.05.2004, 11:41   #3
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 43
Posts: 21,717
Rep Power: 9
Default

Да, в догонку, для тех у кого VC6 int i во втором цикле объявлять не надо, просто i = 0;
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 06:03.
Top

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.