AKB Forums

Go Back   AKB Forums > Technical sections > Algorithms
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Algorithms The source of algorithms for your project

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old May 29, 2004, 10:37   #1
Administrator
 
greka's Avatar
 
Join Date: Sep 2001
Location: @work
Posts: 5,347
Rep Power: 10
Reputation: 23
Send a message via ICQ to greka
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
__________________
И повешенные могут качаться в неположенную сторону. /С.Е.Лец/
greka is offline   Reply With Quote Quote selected
Old May 29, 2004, 11:39   #2
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,637
Rep Power: 11
Reputation: 211
Send a message via ICQ to Agregat Send a message via MSN to Agregat
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
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Old May 29, 2004, 11:41   #3
Грустно...
 
Agregat's Avatar
 
Join Date: Aug 2002
Location: Там, где всегда идут дожди
Posts: 21,637
Rep Power: 11
Reputation: 211
Send a message via ICQ to Agregat Send a message via MSN to Agregat
Да, в догонку, для тех у кого VC6 int i во втором цикле объявлять не надо, просто i = 0;
__________________
http://аvitya.livejournal.com
Хотели, как лучше, а получилось даже хуже...
Лозунг шахматиста: На каждый шах - ответим матом!
Agregat is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Minolta Dimage A2 Forever Child Art 15 Feb 17, 2004 07:08
PHP: Header and Cookies problem Aram Ghazanchyan Web Development 9 Jun 13, 2003 06:48
mail lists management noch Web Development 4 Apr 14, 2003 17:10
Bit packing best algorithm greka Algorithms 27 May 24, 2002 13:01
PAPER A4 FORMAT Vahag Sell and Purchase 0 Mar 2, 2002 03:59


All times are GMT. The time now is 14:19.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link