Sponsored Content
Full Discussion: Number to bit vector
Top Forums Programming Number to bit vector Post 302967319 by migurus on Monday 22nd of February 2016 04:36:20 PM
Old 02-22-2016
I used this function for ages, don't even remember its origin:

Code:
char *conv_to_zeroes_ones(void *in)
{
  register int  i, pos;
  unsigned int  *input;
  char          bit;
  char          buf[256];
         input = (unsigned int *)in;
        memset(buf, 0, 256);
        pos = 0;
         for(i = 31; i >= 0; i--)
        {
                if(((*input >> i) & 1))
                        bit = '1';
                else
                        bit = '0';
                buf[pos++] = bit;
                if((i > 0) && (i % 4) == 0)
                {
                        if((i % 8) == 0)
                        {
                                buf[pos++] = ' ';
                                buf[pos++] = ':';
                                buf[pos++] = ' ';
                        }
                        else
                        {
                                buf[pos++] = ' ';
                        }
                }
        }
        return buf;
}

the result looks like this:

Code:
 
 $ bits 1234
    1234 = 0000 0000 : 0000 0000 : 0000 0100 : 1101 0010

This User Gave Thanks to migurus For This Post:
 

6 More Discussions You Might Find Interesting

1. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies

2. Shell Programming and Scripting

Help needed in converting number to bit

Hi, I have the following file input:- 542 4795 61 543 4795 61 544 0 0 545 292 ... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

3. Programming

Help with bit vector and setting its bits?

void shuffle (card_t cards, card_t cards2, char *bitvec) { int i; unsigned char temp, r; srand(time(NULL)); for (i = 0; i < 52; i++) { r = rand() % 52; temp = bitvec; if (!(temp & (1 << (8- (r % 8))))) { temp |= (1 << (8- (r % 8)));------->is this my problem? } } } ok so I am... (1 Reply)
Discussion started by: bjhum33
1 Replies

4. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

5. Programming

How bit representation of a number can be done in c?

I have an assignment in which a character is the input of which some bits(from a position to certain position) are to be inverted (1's complement) and then the resultant character is to be returned....for example unsigned char x = J from p = 3 to offset n = 5 01001010 inverted to... (1 Reply)
Discussion started by: ezee
1 Replies

6. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies
SHA256(3)						   BSD Library Functions Manual 						 SHA256(3)

NAME
SHA256_Init, SHA256_Update, SHA256_Final, SHA256_End, SHA256_File, SHA256_FileChunk, SHA256_Data -- calculate the FIPS 180-2 ``SHA-256'' mes- sage digest LIBRARY
Message Digest (MD4, MD5, etc.) Support Library (libmd, -lmd) SYNOPSIS
#include <sys/types.h> #include <sha256.h> void SHA256_Init(SHA256_CTX *context); void SHA256_Update(SHA256_CTX *context, const unsigned char *data, size_t len); void SHA256_Final(unsigned char digest[32], SHA256_CTX *context); char * SHA256_End(SHA256_CTX *context, char *buf); char * SHA256_File(const char *filename, char *buf); char * SHA256_FileChunk(const char *filename, char *buf, off_t offset, off_t length); char * SHA256_Data(const unsigned char *data, unsigned int len, char *buf); DESCRIPTION
The SHA256_ functions calculate a 256-bit cryptographic checksum (digest) for any number of input bytes. A cryptographic checksum is a one- way hash function; that is, it is computationally impractical to find the input corresponding to a particular output. This net result is a ``fingerprint'' of the input-data, which does not disclose the actual input. The SHA256_Init(), SHA256_Update(), and SHA256_Final() functions are the core functions. Allocate an SHA256_CTX, initialize it with SHA256_Init(), run over the data with SHA256_Update(), and finally extract the result using SHA256_Final(). SHA256_End() is a wrapper for SHA256_Final() which converts the return value to a 65-character (including the terminating '') ASCII string which represents the 256 bits in hexadecimal. SHA256_File() calculates the digest of a file, and uses SHA256_End() to return the result. If the file cannot be opened, a null pointer is returned. SHA256_FileChunk() is similar to SHA256_File(), but it only calculates the digest over a byte-range of the file specified, start- ing at offset and spanning length bytes. If the length parameter is specified as 0, or more than the length of the remaining part of the file, SHA256_FileChunk() calculates the digest from offset to the end of file. SHA256_Data() calculates the digest of a chunk of data in memory, and uses SHA256_End() to return the result. When using SHA256_End(), SHA256_File(), or SHA256_Data(), the buf argument can be a null pointer, in which case the returned string is allo- cated with malloc(3) and subsequently must be explicitly deallocated using free(3) after use. If the buf argument is non-null it must point to at least 65 characters of buffer space. SEE ALSO
md4(3), md5(3), ripemd(3), sha(3) HISTORY
These functions appeared in FreeBSD 6.0. AUTHORS
The core hash routines were implemented by Colin Percival based on the published FIPS 180-2 standard. BUGS
No method is known to exist which finds two files having the same hash value, nor to find a file with a specific hash value. There is on the other hand no guarantee that such a method does not exist. BSD
March 28, 2014 BSD
All times are GMT -4. The time now is 08:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy