Ir para conteúdo
Fórum Script Brasil
  • 0

Bitis...


Grec

Pergunta

6 respostass a esta questão

Posts Recomendados

  • 0

Cara,

valeu pela ajuda...

Mas, o link que você me deu é sobre ler os bits...Ah, consegui compilar usando a função outport no turboc++ no Borland não deu, nem sei porque...

E a estória de configuração é pro exemplo criar uma variavel "char a" e e colocar para que os bits dela fossem 0100010....

Tomara que possa ajudar.... rolleyes.gif

Link para o comentário
Compartilhar em outros sites

  • 0

Estou mostrando um código de impressão para você avaliar.Basta converter para o C da Borland.Eu já tinha modificado para o Borland 3.1 , mas não achei o fonte.

Quanto a segunda duvida ainda não entendi.

Voce quer converter algum numero ou caracter para binario ??é isto??

//****************************************************************

#include <stdio.h>

#include <conio.h>

#include <time.h>

#include <stdlib.h>

#define PortAdd 0x0378

#define Data (PortAdd + 0)

#define Status (PortAdd + 1)

#define Control (PortAdd + 2)

bool imprime (char *buffer, int tambuffer);

void main(void)

{

bool ok;

char ch;

char buffer[] = "1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n"

"1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSs\n";

while(! _kbhit())

{

ok = imprime( buffer, (int)sizeof(buffer) );

if (! ok)

{

_cputs("Problema na impressora! C Cancela e qualquer tecla continua");

ch = _getch();

if (ch == 'c' || ch == 'C')

return;

}

}

}

bool imprime (char *buffer, int tambuffer)

{

int cont = 0;

clock_t finish;

unsigned char ready, controlreg,ret;

bool timeout;

controlreg = _inp(Control);

while (cont < tambuffer)

{

timeout = true;

ready = _inp(Status);

if ( (ready & 0xF8) != 0xD8)

return false;

_outp( Data, (int)buffer[cont]);

_outp(Control, (controlreg & 0xfe)); // strobe low

_outp(Control, (controlreg & 0xfe));

_outp(Control, (controlreg | 0x01)); // strobe high

finish = clock() + ((clock_t)100);

while (finish > clock() )

{

ret = _inp(Status);

if ( (ret & 0xF8) == 0xD8)

{

timeout = false;

break;

}

}

if (timeout )

return false;

cont++;

}

return true;

}

Link para o comentário
Compartilhar em outros sites

  • 0

Me explica o que você precisa fazer para ver se eu posso te ajudar.Como você faz para incluir o binario em uma variavel char?? No exemplo que eu lhe passei por exemplo se você ler o registrador de status(ready = _inp(Status) e em ready tiver o valor 0x90 por exemplo , em binario seria 1001 0000 , ou seja bastaria converter o valor recebido em hexa para binario . O contrario (de binario para hexa) eu nunca utilizei

)

Link para o comentário
Compartilhar em outros sites

  • 0

ai vao umas funções para manipular bits:

arquivo header

/*****************************************************************************
*                                                                            *
*  --------------------------------- bit.h --------------------------------  *
*                                                                            *
*****************************************************************************/

#ifndef BIT_H
#define BIT_H

/*****************************************************************************
*                                                                            *
*  --------------------------- Public Interface ---------------------------  *
*                                                                            *
*****************************************************************************/

int bit_get(const unsigned char *bits, int pos);

void bit_set(unsigned char *bits, int pos, int state);

void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned
   char *bitsx, int size);

void bit_rot_left(unsigned char *bits, int size, int count);

#endif
arquivo.c
/*****************************************************************************
*                                                                            *
*  --------------------------------- bit.c --------------------------------  *
*                                                                            *
*****************************************************************************/

#include <string.h>

#include "bit.h"

/*****************************************************************************
*                                                                            *
*  -------------------------------- bit_get -------------------------------  *
*                                                                            *
*****************************************************************************/

int bit_get(const unsigned char *bits, int pos) {

unsigned char      mask;

int                i;

/*****************************************************************************
*                                                                            *
*  Set a mask for the bit to get.                                            *
*                                                                            *
*****************************************************************************/

mask = 0x80;

for (i = 0; i < (pos % 8); i++)
   mask = mask >> 1;

/*****************************************************************************
*                                                                            *
*  Get the bit.                                                              *
*                                                                            *
*****************************************************************************/

return (((mask & bits[(int)(pos / 8)]) == mask) ? 1 : 0);

}

/*****************************************************************************
*                                                                            *
*  -------------------------------- bit_set -------------------------------  *
*                                                                            *
*****************************************************************************/

void bit_set(unsigned char *bits, int pos, int state) {

unsigned char      mask;

int                i;

/*****************************************************************************
*                                                                            *
*  Set a mask for the bit to set.                                            *
*                                                                            *
*****************************************************************************/

mask = 0x80;

for (i = 0; i < (pos % 8); i++)
   mask = mask >> 1;

/*****************************************************************************
*                                                                            *
*  Set the bit.                                                              *
*                                                                            *
*****************************************************************************/

if (state)
   bits[pos / 8] = bits[pos / 8] | mask;
else
   bits[pos / 8] = bits[pos / 8] & (~mask);

return;

}

/*****************************************************************************
*                                                                            *
*  -------------------------------- bit_xor -------------------------------  *
*                                                                            *
*****************************************************************************/

void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned
   char *bitsx, int size) {

int                i;

/*****************************************************************************
*                                                                            *
*  Compute the bitwise XOR (exclusive OR) of the two buffers.                *
*                                                                            *
*****************************************************************************/

for (i = 0; i < size; i++) {

   if (bit_get(bits1, i) != bit_get(bits2, i))
      bit_set(bitsx, i, 1);
   else
      bit_set(bitsx, i, 0);

}

return;

}

/*****************************************************************************
*                                                                            *
*  ----------------------------- bit_rot_left -----------------------------  *
*                                                                            *
*****************************************************************************/

void bit_rot_left(unsigned char *bits, int size, int count) {

int                fbit,
                   lbit,
                   i,
                   j;

/*****************************************************************************
*                                                                            *
*  Rotate the buffer to the left the specified number of bits.               *
*                                                                            *
*****************************************************************************/

if (size > 0) {

   for (j = 0; j < count; j++) {

      for (i = 0; i < (size / 8); i++) {

         /********************************************************************
         *                                                                   *
         *  Get the bit about to be shifted off the current byte.            *
         *                                                                   *
         ********************************************************************/

         lbit = bit_get(&bits[i], 0);

         if (i == 0) {

            /*****************************************************************
            *                                                                *
            *  Save the bit shifted off the first byte for later.            *
            *                                                                *
            *****************************************************************/

            fbit = lbit;

            }

         else {

            /*****************************************************************
            *                                                                *
            *  Set the rightmost bit of the previous byte to the leftmost    *
            *  bit about to be shifted off the current byte.                 *
            *                                                                *
            *****************************************************************/

            bit_set(&bits[i - 1], 7, lbit);

         }

         /********************************************************************
         *                                                                   *
         *  Shift the current byte to the left.                              *
         *                                                                   *
         ********************************************************************/

         bits[i] = bits[i] << 1;

      }

      /***********************************************************************
      *                                                                      *
      *  Set the rightmost bit of the buffer to the bit shifted off the      *
      *  first byte.                                                         *
      *                                                                      *
      ***********************************************************************/

      bit_set(bits, size - 1, fbit);

   }

}

return;

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      651,9k
×
×
  • Criar Novo...