Search site Dead Hackers Society
Other BBSes »
 
 

Sommarhack 2017
Demoscene   º   Coding   º   CT60   º   Buy/sell   º   Misc/crap   º   Alive magazine   º   SNDH/Tracking

Misc/crap BBS
 
c-struct with python
Posted by: chemmad Oct,14.2015-09:49 

; argv extracter for 4 parameters a b c d
import struct
from sys import argv
file1 = open("TestFile.txt","w")

istr = [100, 200, 300, 400 ]


for i in range(1,5):
istr[i-1]=float(argv[i])
a=argv[i].encode('hex')
print a
print a.decode('hex')
print >>file1, argv[i]
file1.close()

; possible output


-5252273881777303236
924080823116197693

; make c compatable bin file for c-array
fod = open("writebinary.bin", "wb")
for i in range(1,5):
itext = struct.pack("d", istr[i-1])
a= struct.unpack("d",itext)
fod.write(itext)

fod.close()


3.14159 /* just two from the 4 floats in
0.26535e-5 /* the file .bin
$ hexdump -C const_pi

; print columns 2-9, columns 10-17 with awk make hex varcharstrings for python
here are the awk arguments to get lines 21..27:2..9:10..17
-F " " 'NR>21 && NR <25 {print $10$11$12 $13$14$15$16$17}'
-F " " 'NR>18 && NR <27 {print $2$3$4$5$6$7$8$9}'

; shows how to hexdumpto float in python

cat pi.py
import struct /* use funclib struct */
a="6e861bf0f9210940" /* hexdump col. feeds decode()
b="26d2590d5942c63e"
c="79fff182cf2eb13c"
d="552ab9dd795ca03a"
A=struct.unpack("d", a.decode('hex'))[0] /* struct.unpack() make strings*/
B=struct.unpack("d", b.decode('hex'))[0]
C=struct.unpack("d", c.decode('hex'))[0]
D=struct.unpack("d", d.decode('hex'))[0]
fa=float(A) /* feed strings in float(() */
fb=float(B)
fc=float(C)
fd=float(D)
print fa
print fa+fb+fc+fd /* sums floats */

// array with 4 elements stored in writebinary.bin
// title="readbinary"
// main.c
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
FILE *fid; double *a;

fid = fopen("writebinary.bin","r");
a = (double*) calloc(4, sizeof(double));
fread(a, sizeof(double), 4, fid);
int i; for(i=0; i<4; i++)
printf("%G ", a[i]);
fclose(fid);
return(1);
}






[All messages in this thread]

Topic Posted by  Date 
c-struct with python chemmad Oct,14.2015-09:49
  append double number chemmad Oct,23.2015-09:14


Reply to this message
Name:
Topic:
Human verification (enter the text of the ASCII-art below):

       _       _ _             _   
 _____|_|___ _| | |_ ___ _____| |_ 
|     | |   | . | . | . |     | . |
|_|_|_|_|_|_|___|___|___|_|_|_|___|
Message:




© 1994-2016 Dead Hackers Society Contact: Anders Eriksson