0

I would like to extract text from a textbox and use it as a hexadecimal:

When someone types in "80" into the textbox, I want to be converted from string to hex "0x80", not "0x38 0x30".

For example, input string "E" will be converted to hex "0E". Not "45".

Sorry if this question has been asked before, all I could find were converting from one type to another (which changes the digits) and that is not what I want.

This is my expected results:

Input: 80 (type string, literally just two digits 80 but in string format)

Expected output: 80 (type byte)

14
  • 1
    Please write your input and your expected output. Commented Jul 15 at 8:24
  • 1
    There are built-in methods like Byte.Parse or Convert.ToByte and several duplicates. Performance depends on what the actual string looks like and what you want to do. Things have changed since the 2010s though with the introduction of ReadOnlySpan<char> which can read characters from a long string without making copies. Commented Jul 15 at 8:32
  • 2
    "hex 0E" is literally the same as saying 14; but you've also said that the expected output of "80" is 80; do you actually mean that the expected output of "80" is 128, i.e. 0x80 / "hex 80" ? Commented Jul 15 at 8:44
  • 1
    I really do think, there is a misconception of what a "hexadecimal value" is on your part. The string "80" can and will be converted to the value 0x80 as described in Marc's answer. The value of 0x80 is the same - identical - to decimal 128. Commented Jul 15 at 8:50
  • 1
    There's no yes but no. It's math. Debug.Assert(0x80==128) passes, because 0x80==128 is true. Bot are integer literals representing the same value. You already got the answer you want. All answers produce the same bytes. from what i understand, if i type in 128 not, that's not what people said at all Commented Jul 15 at 8:52

1 Answer 1

2

There is no "hex" data type, so you can't end up with "hex 0x80". If you want to interpret it as an integer from hex notation, i.e. you want the answer to be 128, then:

string input = "80";

int parsed = Convert.ToInt32(input, fromBase: 16); // or ToByte, etc

Console.WriteLine(parsed); // 128

If you then want to show parsed in hex, then you need to reverse that:

Console.WriteLine(Convert.ToString(parsed, toBase: 16)); // 80
// or use the X2 format specifer
Console.WriteLine(parsed.ToString("X2")); // 80

If you expect the output to be 80 (from question)

Expected output: 80 (type byte)

Then... forget about the "hex" aspect and just use byte.TryParse or similar.

Sign up to request clarification or add additional context in comments.

15 Comments

Hi, thank you! but i do not want the answer to be 128. but 80 (same digits but in bytes format, not string)
There is no "Bytes format". 0x80 is a hexadecimal value. Which is the decimal value of 128.
Yeah. I want my string "80" to be converted to hexadecimal value "0x80". Not decimal value 128
You cannot. 80 hex === 128 dec. If it's not dec 128, it's also not 0x80.
If you want the output from "80" to be "0x80", then... you seem to be thinking of strings, in which case: string output = "0x" + input; - if you are actually thinking of "80" as hex, as a primitive, then: that is 128, i.e. 0x80; sorry, but that's simply what hex means. The primitive value does not have a base (hex, decimal, etc) - the base is only relevant when rendering an integer to readable text. The integer is the same value regardless of whether it is written as decimal, hex, octal, binary, etc. And the value of 0x80 is the integer 128
Thank you, this is painting a clearer picture for me now. I need 0x80 or 0x85 (in bytes format) as an input on my second function, but i need the user to key in whether they want to do 80 or 85. having them key in 128 or 133 may seem confusing to them so i want them to be able to key in 80 or 85 then convert it in my code. sorry, im not very good at explaining
what does "bytes format" mean? that's not a well-defined thing, so clarifying exactly what you mean would really help; are you passing a value to a byte parameter? if so: the byte you want to send will have the value 128, i.e. 0x80
If I may suggest: Then make it a Selection Component and offer "80" and "85" as Options to choose from for the user. What that ends up using for subsequent functions is up to you then. But it also will prevent the user from entering -5 or 42 ...
@Marc Sorry i'm just so confused about this whole bytes and hexadecimal thing (are they the same thing? are they formats? object types?) but all i'm sure of is, i want the user-typed 80 (Textbox.Text, so string) to be converted to 0x80 (byte[]) (correct me if im wrong but, byte[] are values in hexadecimal?) but i guess theres no direct way to convert it directly so i probably will have to map 80 (string) to 128 (string) then convert that into the hex equivalent 0x80 which can then be passed as a byte[]. thank you so much for your kind and patient replies and help
@ Fildor thanks for the suggestion! i was going to make it a selection but i thought it would still be string format anyway when i asked the question so i was wondering how to do the conversion from there. didnt think of mapping 80 to the actual decimal value 128 of the hex i want (0x80) before conversion to 0x80. just thought of 80 to 0x80 straight.
@winter-raptor0411 "correct me if im wrong but, byte[] are values in hexadecimal?" - Byte values don't conform to a specific format. The reason they're often written in hexadecimal is just to make them easier to read by a human (you). But to the computer it all looks the same - everything is in binary to it. 0x80 (hexadecimal), 128 (decimal) and 1000 0000 (binary) are all the same. -- If you want the user to be able to type "80" or "85", then you should use the code that Marc wrote in his answer: int parsed = Convert.ToInt32(input, fromBase: 16);
Here's a similar scenario that might help you get a better understanding: A butterfly has different names in different languages - it can be called "Butterfly" (English), "Fjäril" (Swedish) or "Schmetterling" (German). Regardless of which word you decide to use it is still the same animal.
Integers do not have any base (well, technically they are stored as binary base 2 values). The only time you need to worry about the base is when you want to convert them to or from a string.
Why not just Convert.FromHexString then it can parse multiple bytes
because OP has only ever discussed a single byte; that would indeed be a great option if the requirement is to read multiple hex tokens

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.