I rarely do scripting and I have problem using reg expression using Jmeter's beanshell.
Basically, I have an output of 16 digits. I wanted to search the string, separate every 2nd character, and prepend with '0x' followed by a space, repeat for all 16 digits, lastly using vars.put into a self defined parameters. I have managed to do this in bash easily but not able to find solution with Beanshell Jmeter.
// get current date
var d = new Date();
// convert to epoch
var militime = d.getTime();
// convert to 16digit
//var nowtime1 = (long.valueOf(militime) * 1000);
var nowtime1 = militime * 1000;
var nowtime1 = nowtime1.replace(/({0,2})/g,"x0"); //I'm unsure of the syntax here
Does anyone has suggestion to improve?
This is sample output from my bash script using sed to format:
1495207922508835
0x14 0x95 0x20 0x79 0x22 0x50 0x88 0x35
Using Beanshell is some form of antipattern when it comes to creating JMeter scripts, you should consider using JSR223 Test Elements and Groovy language instead as Groovy is less resource intensive, has much better performance and it is more Java-compliant.
Coming back to your question, Groovy implementation of your requirement would be something like:
def now = "1495207922508835"
def result = new StringBuilder()
now.split("(?<=\\G..)").each {
result.append("0x").append(it).append(" ")
}
log.info(result.toString())
Demo:
See Groovy is the New Black guide to learn about why and how you should be using Groovy in JMeter tests.
NB: adding 0x doesn't really convert an integer into hex, you might want to consider Integer.toHexString() method instead.
I would simplify current time retrieval to System.currentTimeMillis() * 1000. Second, you are trying to manipulate number as if it's a string, but then you need to convert it to string. Regex itself is almost right. So the whole script looks like this:
now = "" + (java.lang.System.currentTimeMillis() * 1000);
result = now.replaceAll("(.{2})", "0x$1 ");
Example:
now result
1495215390956000 0x14 0x95 0x21 0x53 0x90 0x95 0x60 0x00
1495215441281000 0x14 0x95 0x21 0x54 0x41 0x28 0x10 0x00
Related
I have a Python3 function that combine two bytes, one use bytes.fromhex() method, and the other use to_bytes() method:
from datatime import datetime
def bytes_add() -> bytes:
bytes_a = bytes.fromhex('6812')
bytes_b = datetime.now().month.to_bytes(1, byteorder='little', signed=False)
return bytes_a + bytes_b
Is it possible to write a same function as above in Raku?(if so, How to control byteorder and signed params?)
as for byteorder, say convert number 1024 to bytes in Python:
(1024).to_bytes(2, byteorder='little') # Output: b'\x00\x04', byte 00 is before byte 04
as a contrast, convert number 1024 to Buf or Blob in Raku:
buf16.new(1024) # Output: Buf[uint16]:0x<0400>, byte 00 is after byte 04
is there any way to get Buf[uint16]:0x<0004> in the above example in Raku?
Update:
inspired by codesections, I try to figure out a solution similar to codesections's answer:
sub bytes_add() {
my $bytes_a = pack("H*", '6812');
my $bytes_b = buf16.new(DateTime.now.month);
$bytes_a ~ $bytes_b;
}
But still don't know how to use byteorder.
Is it possible to write a same function as above in Raku?
Yes. I'm not 100% sure I understand the overall goal of the function you provided, but a literal/line-by-line translation is certainly possible. If you would like to elaborate on the goal, it may also be possible to achieve the same goal in an easier/more idiomatic way.
Here's the line-by-line translation:
sub bytes-add(--> Blob) {
my $bytes-a = Blob(<68 12>);
my $bytes-b = Blob(DateTime.now.month);
Blob(|$bytes-a, |$bytes-b)
}
The output of bytes-add is printed by default using its hexadecimal representation (Blob:0x<44 0C 09>). If you'd like to print it more like Python prints its byte literals, you can do so with bytes-add».chr.raku, which prints as ("D", "\x[C]", "\t").
if so, How to control byteorder?
Because the code above constructs the Blob from a List, you can simply .reverse the list to use the opposite order.
I imported a cvs file and have a variable 'departure time' in my sas file that consists of four digits, e.g. 0856 for 08.56 AM. I would like SAS to recognise it as time and want it to appear as 08:56. I have tried:
put DEP_TIME hhmm5.;
format DEP_TIME hhmm5.;
Doesn't work. Can't seem to figure this out.
Any clues?
Informat B8601TM.
33 data _null_;
34 t='0856';
35 time = input(t,B8601TM.);
36 format time time.;
37 put 'NOTE: ' (_all_)(=);
38 run;
NOTE: t=0856 time=8:56:00
I don't think there's an informat which will convert a 4-digit string to a time.
There's a couple of ways to do this, either using hms() and substr() functions, or a custom picture format :
proc format ;
picture TM
low-high = '00:00' ;
run ;
data want ;
set have ;
/* hms and substr method */
new_time1 = hms(substr(dep_time,1,2),substr(dep_time,3,2)) ;
format new_time1 hhmm5. ;
/* input / put with picture format */
new_time2 = input(put(input(dep_time,4.),tm.),time5.) ;
format new_time2 hhmm5. ;
run ;
I have a .bin file, and I want to simply byte reverse the hex data. Say for instance # 0x10 it reads AD DE DE C0, want it to read DE AD C0 DE.
I know there is a simple way to do this, but I am am beginner and just learning python and am trying to make a few simple programs to help me through my daily tasks. I would like to convert the whole file this way, not just 0x10.
I will be converting at start offset 0x000000 and blocksize/length is 1000000.
EDIT:
here is my code, maybe you can tell me where i am messing up.
def main():
infile = open("file.bin", "rb")
new_pos = int("0x000000", 16)
chunk = int("1000000", 16)
data = infile.read(chunk)
save(data)
def save(data):
with open("reversed", "wb") as outfile:
outfile.write(data)
main()
how would i go about coding it to byte reverse from CDAB TO ABCD?
if it helps any the file is exactly 16MB
You can just swap the bytes manually like this:
with open("file.bin", "rb") as infile, open("reversed", "wb") as outfile:
data = infile.read()
for i in xrange(len(data) / 2):
outfile.write(data[i*2+1])
outfile.write(data[i*2])
I'm starting to learn UPC, and I have the following piece of code to read a file:
upc_file_t *fileIn;
int n;
fileIn = upc_all_fopen("input_small", UPC_RDONLY | UPC_INDIVIDUAL_FP , 0, NULL);
upc_all_fread_local(fileIn, &n, sizeof(int), 1, UPC_IN_ALLSYNC | UPC_OUT_ALLSYNC);
upc_barrier;
printf("%d\n", n);
upc_all_fclose(fileIn);
However, the output (value of n) is always 808651319, which means something is wrong, and I can't find what is it. The first line of the file I'm giving as input is '7', so the result of the printf should be 7...
Any idea why this happens?
Thanks in advance!
UPC Parallel I/O library performs unformatted (binary) input/output, not formatted one like what you get with (f)printf(3)/(f)scanf(3) from the standard C library. Parallel I/O cannot handle text files because of their intrinsic properties like variable-length records.
upc_all_fread_local(fileIn, &n, sizeof(int), 1, UPC_IN_ALLSYNC | UPC_OUT_ALLSYNC)
behaves like the following call to the standard C library function for unformatted read from a file:
fread(&n, sizeof(int), 1, fh)
You are just reading 1 element of sizeof(int) bytes from the file (4 bytes on most platforms) into the address of n. The number you got 808651319 in hexadecimal is 0x30330A37. On little endian systems like x86/x64 this is stored in memory and on disk as 0x37 0x0A 0x33 0x30 (reversed byte order). These are the ASCII codes of the first 4 bytes of the string 7\n30 (\n or LF is the line feed/new line symbol) so I'd guess your input_small file looked like:
7
30...
...
You should prepare your input data in binary format using fwrite(3) instead of using (f)printf(3) or your text editor of choice.
I'm trying to convert an int into a byte in Processing 1.0.9.
This is the snippet of code that I have been working with:
byte xByte = byte(mouseX);
byte yByte = byte(mouseY);
byte setFirst = byte(128);
byte resetFirst = byte(127);
xByte = xByte | setFirst;
yByte = yByte >> 1;
port.write(xByte);
port.write(yByte);
According to the Processing API, this should work, but I keep getting an error at xByte = xByte | setFirst; that says:
cannot convert from int to byte
I have tried converting 128 and 127 to they respective hex values (0x80 and 0x7F), but that didn't work either. I have tried everything mentioned in the API as well as some other blogs, but I feel like I'm missing something very trivial.
I would appreciate any help.
Thank you.
I've never used Processing before, but it's possible the | operator returns an integer regardless of the arguments' types. Try changing the problematic line to
xByte = byte(xByte | setFirst);