How to deal with Hex values in YAML using both Go and Python?

40
February 08, 2019, at 07:50 AM

I have this simple following program:

package main
import (
    "fmt"
    yaml "gopkg.in/yaml.v2"
)
type Test struct {
    SomeStringWithQuotes string `yaml:"someStringWithQuotes"`
    SomeString           string `yaml:"someString"`
    SomeHexValue         string `yaml:"someHexValue"`
}
func main() {
    t := Test{
        SomeStringWithQuotes: "\"Hello World\"",
        SomeString:           "Hello World",
        SomeHexValue:         "0xDef9C64256DeE61ebf5B212238df11C7E532e3B7",
    }
    yamlBytes, _ := yaml.Marshal(t)
    fmt.Print(string(yamlBytes))
}

This prints the following and obviously demonstrates that Go makes decisions on when to quote a string or not:

someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7

However, when I try to read this YAML using the following Python script:

import yaml
yaml_str = """
someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7
"""
print(yaml.load(yaml_str))

It parses the Hex value as an integer. If I now serialize back to YAML using this code:

import yaml
import sys
yaml_str = """
someStringWithQuotes: '"Hello World"'
someString: Hello World
someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7
"""
print(yaml.dump(yaml.load(yaml_str)))

I get:

someHexValue: 1272966107484048169783147972546098614451903325111
someString: Hello World
someStringWithQuotes: '"Hello World"'

How can I best make sure that the Hex format is preserved? Unfortunately, I personally don't have any influence on the code on the Go side (but a Go-side solution is still welcome for other people who try to do similar things).

Answer 1

Go interprets that hex string as a number.

someHexValue: 0xDef9C64256DeE61ebf5B212238df11C7E532e3B7

If that is the yaml it produces then python is right to treat it as a number.

A band aid for this in python is to convert it back to hex using

hex(1272966107484048169783147972546098614451903325111)

Here is the yaml spec that treats that hex as a number

READ ALSO
PHP Double Quotes and replies [duplicate]

PHP Double Quotes and replies [duplicate]

This question already has an answer here:

47
How do I fix exporting csv from WordPress to server without also offering file to save

How do I fix exporting csv from WordPress to server without also offering file to save

I've managed to create a csv file after the post is published, it is exporting correctly and saving to the serverHowever, it is also opening a dialogue box offering the same file to download/save

34
Why can't I upload files with PHP?

Why can't I upload files with PHP?

I'm having trouble getting PHP file uploads to work on a CentOS serverHere is a very basic version of the script

56
Wordpress Elementor Edit/Customize Widget [on hold]

Wordpress Elementor Edit/Customize Widget [on hold]

Hello Ladies and Gentlemen, i am pretty overwhelmed by some simple thing here and would love to request your help

36