logo
down
shadow

How to put bits into a character Array


How to put bits into a character Array

By : kefirzhang
Date : October 15 2020, 11:12 PM
this one helps. I need to know how to put bits into a character array. , Is that really all?
code :


Share : facebook icon twitter icon
Integer (representing a sequence of bits) reinterpretation as Character Array in Python

Integer (representing a sequence of bits) reinterpretation as Character Array in Python


By : MichaelMurdock
Date : March 29 2020, 07:55 AM
Hope that helps You can use the struct module:
code :
import struct

# Pack a Python long as if it was a C unsigned integer, little endian
bytes = struct.pack("<I", 0x78FF00AA)
print [hex(ord(byte)) for byte in bytes]

['0xaa', '0x0', '0xff', '0x78']
hunting for a particular pair of bits '10' or '01' in a character array

hunting for a particular pair of bits '10' or '01' in a character array


By : kenankomurcu
Date : March 29 2020, 07:55 AM
like below fixes the issue Lookup Table Idea
There are only 256 possible bytes. That is few enough that you can construct a lookup table of all the possible bit combinations that can happen in one byte.
code :
/* each value is the bit position of a possible pattern OR'd with a pattern ID bit. */
/* 0 is no match */
#define P_01 0x00
#define P_10 0x10
const char byte_lookup[256] = {
    /*  0: 0000_0000, 0000_0001, 0000_0010, 0000_0011 */
                   0,    2|P_01,    3|P_01,    3|P_01,
    /*  4: 0000_0100, 0000_0101, 0000_0110, 0000_0111, */
              4|P_01,    4|P_01,    4|P_01,    4|P_01,
    /*  8: 0000_1000, 0000_1001, 0000_1010, 0000_1011, */
              5|P_01,    5|P_01,    5|P_01,    5|P_01,
};
How do i create byte array that contains 64 bits array and how do i convert those bits into hex value?

How do i create byte array that contains 64 bits array and how do i convert those bits into hex value?


By : Nishu
Date : March 29 2020, 07:55 AM
Hope that helps You could use a BitArray:
code :
var bits = new BitArray(64);

bool bit17 = bits[17];
int bitNumber = 7; // For example.
byte value = (byte)(1 << bitNumber);
string hex = value.ToString("x");
Console.WriteLine(hex);
US-ASCII string (de-)compression into/from a byte array (7 bits/character)

US-ASCII string (de-)compression into/from a byte array (7 bits/character)


By : user2945655
Date : March 29 2020, 07:55 AM
it helps some times Here is a quick & dirty solution without any libraries, i.e. only JRE on-board means. It is not optimised for efficiency and does not check if the message is indeed US-ASCII, it just assumes it. It is just a proof of concept:
code :
package de.scrum_master.stackoverflow;

import java.util.BitSet;

public class ASCIIConverter {
  public byte[] compress(String message) {
    BitSet bits = new BitSet(message.length() * 7);
    int currentBit = 0;
    for (char character : message.toCharArray()) {
      for (int bitInCharacter = 0; bitInCharacter < 7; bitInCharacter++) {
        if ((character & 1 << bitInCharacter) > 0)
          bits.set(currentBit);
        currentBit++;
      }
    }
    return bits.toByteArray();
  }

  public String decompress(byte[] compressedMessage) {
    BitSet bits = BitSet.valueOf(compressedMessage);
    int numBits = 8 * compressedMessage.length - compressedMessage.length % 7;
    StringBuilder decompressedMessage = new StringBuilder(numBits / 7);
    for (int currentBit = 0; currentBit < numBits; currentBit += 7) {
      char character = (char) bits.get(currentBit, currentBit + 7).toByteArray()[0];
      decompressedMessage.append(character);
    }
    return decompressedMessage.toString();
  }

  public static void main(String[] args) {
    String[] messages = {
      "Hello world!",
      "This is my message.\n\tAnd this is indented!",
      " !\"#$%&'()*+,-./0123456789:;<=>?\n"
        + "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\n"
        + "`abcdefghijklmnopqrstuvwxyz{|}~",
      "1234567890123456789012345678901234567890"
        + "1234567890123456789012345678901234567890"
        + "1234567890123456789012345678901234567890"
        + "1234567890123456789012345678901234567890"
    };

    ASCIIConverter asciiConverter = new ASCIIConverter();
    for (String message : messages) {
      System.out.println(message);
      System.out.println("--------------------------------");
      byte[] compressedMessage = asciiConverter.compress(message);
      System.out.println("Number of ASCII characters = " + message.length());
      System.out.println("Number of compressed bytes = " + compressedMessage.length);
      System.out.println("--------------------------------");
      System.out.println(asciiConverter.decompress(compressedMessage));
      System.out.println("\n");
    }
  }
}
Hello world!
--------------------------------
Number of ASCII characters = 12
Number of compressed bytes = 11
--------------------------------
Hello world!


This is my message.
    And this is indented!
--------------------------------
Number of ASCII characters = 42
Number of compressed bytes = 37
--------------------------------
This is my message.
    And this is indented!


 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~
--------------------------------
Number of ASCII characters = 97
Number of compressed bytes = 85
--------------------------------
 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~


1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
--------------------------------
Number of ASCII characters = 160
Number of compressed bytes = 140
--------------------------------
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
Count bits in character and tally the bits

Count bits in character and tally the bits


By : emma19
Date : March 29 2020, 07:55 AM
help you fix your problem The individual bits of a word are not directly accessible with C, so we have to do other things to get at them.
Consider your values, which give which bit is set:
Related Posts Related Posts :
  • Finding a substring, using pointers
  • How to get metadata from a file in c
  • How to define a struct of several strings in it?
  • Reading columns of strings and integers from file
  • Flex/bison keeps on giving me syntax error
  • Linked list of chars in C
  • Why does this small C program crash on ESP32?
  • How do I limit the input of scanf to integers and floats(numbers in general)
  • printf is not giving output when using system
  • Running a particular c file in linux
  • C error expected specifier-qualifier-list before ‘time_t’
  • How to format integer correctly in c?
  • Dynamic relocation of code section
  • Efficiently Traverse Directory Tree with opendir(), readdir() and closedir()
  • How am I able to access a static variable from another file?
  • srandom( time( NULL ) )
  • Looping threads accessing pthread mutex
  • sYSMALLOc: Assertion failed - any ideas how to debug effectively?
  • Auto generate header files for a C source file in an IDE
  • printf and ++ operator
  • feeding a mouse driver alternate data in linux
  • Random int 1-255 to character in C
  • Problems with File I/o when porting old 'C' libraries from 32-bit to 64-bit
  • Static / global variables in shared library in C language
  • Is there a tool to list global variables used and output by a C function?
  • functions returning char pointer
  • How many elements are full in a C array
  • Resources for writing kernel in C
  • OpenGL: How do I avoid rounding errors when specifying UV co-ordinates
  • Basic skeleton for a C OpenGL program on OSX
  • Float versus integer data type calculation issues in C
  • What have you used sysv/posix message queues for?
  • Recursive harmonic function returns NaN
  • How to fix "unable to open stdio.h in Turbo C" error?
  • C programming structure passed as cast to char *?
  • Do I need to #include <omp.h> in my C/C++ sources?
  • Find original owning process of a Linux socket
  • How can I send e-mail in C?
  • How is the lagged fibonacci generator random?
  • Trouble with dangling pointers and character arrays in C
  • Trying to compile with cl.exe
  • C code compilation failure
  • Gtk Draw Bitmap
  • booting from a disk/cd/usb
  • Due to Shared memory, when MS Visual C 6.0 DLL crashes it also causes VB 6 EXE to crash
  • Error receiving in UDP: Connection refused
  • How to get the port number from addrinfo in unix c
  • Should I declare the expected size of an array passed as function argument?
  • Is it possible to read in a string of unknown size in C, without having to put it in a pre-allocated fixed length buffer
  • How to use pcap_breakloop?
  • C Programming linux , read system inputs like ping or ls -l
  • Unsigned Short to Unsigned Long assignment
  • C: strtok on pointer inside a struct
  • C moving the cursor to the beginning of the current line
  • libgtk version issue on ubuntu
  • creating a list of queues
  • Pointer to a specific fixed address
  • How to parse HTTP responses in C?
  • Use strtol or strtok to parse a string in C?
  • fread/fwrite string in C
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org