How do I add to a hex number?

2

I am trying to implement the following lines in python to C#

final_reg = hex(gain_st)+'400020'

After that, I need to write final_reg to a register which in python was written this way.

serial.write(int('A', 16), int(final_reg, 16))

But In C# I implemented my serial.write using a function called write_word_segment_addr(); For Ex, if I want to write 90 to A I write like this currently:

mem.write_word_segment_addr(0x7c032, 0x00000090);      //A

How do I convert gain_st which is a UInt32 to hex and then add 400020 and then convert it to 0x format seen above so that I may write to my register?

c#
asked on Stack Overflow Sep 27, 2019 by Alex Callaway • edited Sep 27, 2019 by Alex Callaway

2 Answers

1
final_reg = gain_st.ToString("x") + "400020";
mem.write_word_segment_addr(10, int.Parse(final_reg, NumberStyles.HexNumber));
answered on Stack Overflow Sep 27, 2019 by shingo • edited Sep 27, 2019 by shingo
0

In case of C# you can use string interpolation:

 // 0x is not required for further conversion and can be dropped
 string final_reg = $"0x{gain_st:x}{400020}";

If you want to convert final_reg to integer, use Convert:

 write_word_segment_addr(Convert.Int32("A", 16), Convert.Int32(final_reg, 16));
answered on Stack Overflow Sep 27, 2019 by Dmitry Bychenko

User contributions licensed under CC BY-SA 3.0