How to convert integer to fixed length hex string in Go? - string

I want to convert an integer to a hex string with a fixed length of 64 characters, prepended with zeros for integer values that do not use up all 32 hex values. If I try the following it adds spaces in front of s rather than zeros.
i := 898757
s := fmt.Sprintf("%64x", i)
fmt.Println(s)

The correct format is "%064x":
fmt.Printf("%064x\n", 898757)
00000000000000000000000000000000000000000000000000000000000db6c5
Where the leading 0 is a "flag" for the formatting string. Per the fmt docs:
0: pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign
My personal preference is to use a period to separate the flags from the length field. This technically works because . is not meaningful with the integer verbs and is ignored. I find it a useful visual indicator. The format string becomes "%0.64x".

Related

Binary Formatting Variables in TCL

I am trying to create a binary message to send over a socket, but I'm having trouble with the way TCL treats all variables as strings. I need to calculate the length of a string and know its value in binary.
set length [string length $message]
set binaryMessagePart [binary format s* { $length 0 }]
However, when I run this I get the error 'expected integer but got "$length"'. How do I get this to work and return the value for the integer 5 and not the char 5?
To calculate the length of a string, use string length. To calculate the length of a string in a particular encoding, convert the string to that encoding and use string length:
set enc "utf-8"; # Or whatever; you need to know this ahead of time for sanity's sake
set encoded [encoding convertto $enc $message]
set length [string length $encoded]
Note that with the encoded length, this will be in bytes whereas the length prior to encoding is in characters. For some messages and some encodings, the difference can be substantial.
To compose a binary message with the length and the body of the message (a fairly common binary format), use binary format like this:
# Assumes the length is big-endian; for little-endian, use i instead of I
set binPart [binary format "Ia*" $length $encoded]
What you were doing wrong was using s* which consumes a list of integers and produces a sequence of little-endian short integer binary values in the output string, and yet were feeding the list that was literally $length 0; and the string $length is not an integer as those don't start with $. We could have instead done [list $length 0] to produce the argument to s* and that would have worked, but that doesn't seem quite right for the context of the question.
In binary format, these are the common formats (there are many more):
a is for string data (mnemonically “ASCII”); this is binary string data, and you need to encode it first.
i and I are for 32-bit numbers (mnemonically “int” like in many programming languages, but especially C). Upper case is big-endian, lower case is little-endian.
s and S are for 16-bit numbers (mnemonically “short”).
c is for 8-bit numbers (mnemonically “char” from C).
w and W are for 64-bit numbers (mnemonically “wide integers”).
f and d are for IEEE binary floating point numbers (mnemonically “float” and “double” respectively, so 4 and 8 bytes).
All can be followed by an optional length, either a number or a *. For the number ones, instead of inserting a single number they insert a list of them (and so consume a list); numbers give fixed lengths, and * does “all the list”. For the string format indicator, a number uses a fixed number of bytes in the message (truncating or padding with zero bytes as necessary) and * does “all the string” (never truncating or padding).

Convert an int to hex and then pad it with 0's to get a fixed length String

I'm having some issues on trying to convert an int to hex then, padding it with 0s in order to get a 6 Characters String which represents the hex number.
So far, I tried the following:
intNumber := 12
hexNumber := strconv.FormatInt(intNumber, 16) //not working
And then I found out how to pad it with 0s, using %06d, number/string. It makes all the strings 6 characters long.
Here you can Find a Playground which I set up to make some tests.
How can I achieve this in a efficient way?
For any Clarifications on the question, just leave a comment below.
Thanks In advance.
import "fmt"
hex := fmt.Sprintf("%06x", num)
The x means hexadecimal, the 6 means 6 digits, the 0 means left-pad with zeros and the % starts the whole sequence.

Convert substring to int in golang

I have this variable:
danumber := "542353242"
and want to extract a character from the string and operate with it as a number.
Tried this:
int(danumber[0])
but it doesn't seem to work.
What your expression gives you is the character code for the digit. To convert the character to the character's value, subtract 0's character code from it:
int(danumber[0] - '0') // in your example, this is: 53 - 48
If you want to convert multiple digits, I would recommend using the strconv package:
number, err := strconv.Atoi(danumber[0:2]) // convert first two characters to int

Oracle, adding leading zeros to string (not number)

I am using Oracle (work space is TOAD) and I need to make my strings that if they are shorted then 10 characters then add leading zeros to make them all 10 digit strings.
For example if I have a string like this:
'12H89' need to be '0000012H89'
or
'1234' to be '0000001234'
How can this be done? Whats the best way?
Thanks in advance .
You can use the LPAD function for that, passing in the string, the length you want it to be, and the character to pad it with. For 10 digits with leading zeroes this would be:
LPAD('12H89', 10, '0')
The return value is the padded string.
See: http://www.techonthenet.com/oracle/functions/lpad.php

transform string/char to uint8

Why does the expression:
test = cast(strtrim('3'), 'uint8')
produce 51?
This is also true for:
test = cast(strtrim('3'), 'int8')
Thanks.
Because 51 is the ASCII code for the character '3'.
If you want to transform the string to numeric 3, you should use
uint8(str2double('3'))
Note that str2double will ignore trailing spaces, so that strtrim isn't necessary.
EDIT
When a string is used in an numeric operation, Matlab automatically converts it to its ASCII value. For example
>> '1'+1
ans =
50
Because 51 is the ASCII value for the character '3'.
This is because '3' is seen as an ASCII character to matlab. By casting as a signed or unsigned integer (8 bits in this case) you are asking Matlab to convert an ASCII '3' to a decimal number. In this case the decimal number is 51. If you want to look at more conversions here is a basic document.

Resources